在猫鼬交易中使用`create()`的验证错误 [英] Validation error using `create()` in Mongoose transaction

查看:87
本文介绍了在猫鼬交易中使用`create()`的验证错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用猫鼬测试交易,并尝试使用以下代码完成一个非常简单的任务:

I'm testing transactions using mongoose and trying to accomplish a very simple task with the following code:

      const session = await mongoose.startSession();
      session.startTransaction();
      try {
        const opts = { session, new: true };
        const A = await Author.
        create({
          firstName: 'Jon',
          lastName: 'Snow'}, session);
        if(!A) throw new Error('Aborted A by Amit!');
        const B = await Author.
        findOneAndUpdate({firstName: 'Bill'}, {firstName: 'William'}, opts);
        if(!B) throw new Error('Aborted B by Amit!');
        await session.commitTransaction();
        session.endSession();
      } catch (err) {
        await session.abortTransaction();
        session.endSession();
        throw err;
      }

我要做的就是首先(使用mongoose create()方法)将一个新文档插入集合中,然后(使用Mongo findOneAndUpdate()方法)编辑同一集合中的另一个文档.任何一个查询失败都需要中止整个交易套件.

All I'm trying to do is first insert (using mongoose create() method) a new document into a collection and then edit (using Mongo findOneAndUpdate() method) another document in the same collection. Failure of either query needs to abort the entire transaction suite.

create()似乎给我带来了问题.确实创建并插入了文档,但是,它也会引发错误:

It's the create() that seems to be giving me problems. The document does get created and inserted, however, it also throws an error:

作者验证失败:姓氏:路径lastName是必需的. firstName:路径firstName是必需的."

"Author validation failed: lastName: Path lastName is required., firstName: Path firstName is required."

任何想法都意味着什么?似乎有人在抱怨我没有给required字段(firstNamelastName)提供值.

Any idea what this could mean? It seems it's complaining about not being given values for required fields (firstName and lastName) despite me having already given it those.

推荐答案

我不知道为什么当我同时提供缺失的值和它们仍被添加到集合中时,为什么会抱怨缺失的值!

I have no idea why it would complain about missing values when I've provided them both and they're still getting added to the collection!

这是因为 Model.create()第一个参数接受文档以插入,作为数组或散布.例如,这两个等效:

This is because Model.create() first parameters accept documents to insert, as an array OR as a spread. For example, these two are equivalent:

// pass a spread of docs
Candy.create({ type: 'jelly bean' }, { type: 'snickers' }) 

// pass an array of docs 
Candy.create([{ type: 'jelly bean' }, { type: 'snickers' }])

您的行的问题是,它试图将第二个文档{session: session}用作Author的另一个条目,该文档缺少必需的firstNamelastName字段.

The problem with your line, is that it's trying to take the second document {session: session} as another entry for Author, which is missing the required firstName and lastName fields.

相反,您应该这样做:

Author.create([{ firstName: 'Quentin', lastName: 'Tarantino' }], 
               { session: session }
);

您可能还会发现猫鼬中的交易是有用的参考.

You may also find Transactions in Mongoose a helpful reference.

这篇关于在猫鼬交易中使用`create()`的验证错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆