简体   繁体   English

mongoose findOneAndUpdate为err和doc返回null(使用Jest)

[英]mongoose findOneAndUpdate returns null for both err and doc (using Jest)

I'm using Mongoose's findOneAndUpdate method, and can't seem to get any useful information back from it. 我正在使用Mongoose的findOneAndUpdate方法,似乎无法从中获取任何有用的信息。 I've tried using the Query that comes back from calling it (in updatedUser ), but it returns null ; 我已经尝试使用从调用它(在updatedUser )返回的Query,但它返回null ; I've also tried including the callback argument, but when I do that I get null back for both err and doc . 我也尝试过包含回调参数,但是当我这样做时,我会为errdoc得到null

Context: I'm using Jest to build a test suite, and writing a test for this function. 上下文:我正在使用Jest构建测试套件,并为此函数编写测试。 I have had the function working in my application outside of the test suite, and while I have changed parts of the function I haven't changed anything in the findOneAndUpdate call, except for adding the callback argument. 我已经在测试套件之外的应用程序中使用了该函数,虽然我已经更改了函数的一部分,但我没有在findOneAndUpdate调用中更改任何内容,除了添加回调参数。

Expected behavior: Either 'err' or 'doc' doesn't return null so I at least know what's happening. 预期的行为:'err'或'doc'都不会返回null,所以我至少知道发生了什么。

I've worked with Mocha and I'm learning Jest, and I've discovered a couple of other entertaining things that Jest does. 我和Mocha一起工作,我正在学习Jest,而且我发现了Jest所做的其他一些有趣的事情。 I know from experience that this is most likely me missing something fairly obvious, but I'd love to be able to blame something else for once. 我从经验中知道,这很可能是我错过了一些相当明显的东西,但我很想能够一次归咎别的东西。 :) :)

updateUser.js: updateUser.js:

const updateUser = async (user) => {

  console.log('user in updater:', user);

  const updatedUser = await User.findOneAndUpdate(
    { _id: user._id },
    {
      $set: {
        name: user.name,
        email: user.email,
        password: user.password,
        ageRange: user.ageRange,
        gender: user.gender,

        accountCreatedAt: user.accountCreateAt,
        meetingPlaces: user.meetingPlaces,
        flags: user.flags,
        tokens: user.tokens,
      }
    },
    { new: true },
    (err, doc) => {
      console.log(err, doc);
    }
  );

  //placeholders until I get some kind of usable response from findOneAndUpdate
  return {err: false, res: 'cat'};
};

terminal output: 终端输出:

 console.log server/db/crud_Users/updateUser.js:8
   user in updater: { _id: 5b1aa9a146b59048dd86dbc2,
     name: 'test',
     email: '24479981326297030000@test.com',
     password: 'testtest',
     ageRange: '1',
     gender: 'Female',
     accountCreatedAt: '2018-06-08T11:06:57-05:00',
     meetingPlaces: [],
     flags: [],
     tokens: [],
     homeLocations: [],
     hostedEvents: [],
     attendingEvents: [],
     __v: 0 }

 console.log server/db/crud_Users/updateUser.js:28
   null null

 console.log server/db/crud_Users/updateUser.js:32
    updatedUser: null

I thank you for explaining the issue that well. 我感谢你解释这个问题。 It is mentioned in the official documentation of MongoDB here that findOneAndUpdate updates the first document in the collection that matches the request and return the original document. 它是MongoDB的官方文档中提到这里findOneAndUpdate相匹配的请求并返回原始文档集合中更新的第一个文档。

I supposed that both err and doc are null because of the document that you want to update doesn't exist in your collection. 我认为errdoc都是null,因为您要更新的文档在您的集合中不存在。 It is not mentioned in the documentation what is the behavior of findOneAndUpdate if there is no document that matches the query. 如果没有与查询匹配的文档,则文档中未提及findOneAndUpdate的行为是什么。 So, I tried to go deeper into it. 所以,我试图深入研究它。

I saw in the documentation (you can learn more about it here ) that if the parameter upsert isn't mentioned, it will take false as a default value. 我在文档中看到了(你可以在这里了解更多),如果没有提到参数upsert ,它将把false作为默认值。 And I saw here in the PS part that if upsert is false and the document doesn't exist, than doc will be null . 我在PS部分看到,如果upsertfalse且文档不存在,则doc将为null

EDIT: not correct - see other answer. 编辑:不正确 - 请参阅其他答案。 I got it figured out. 我明白了。 I was returning the variable asynchronously before it had a value, so it wasn't a findOneAndUpdate issue after all. 我在它有一个值之前异步返回变量,所以它毕竟不是一个findOneAndUpdate问题。 :/ :/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM