简体   繁体   English

Promise.all返回双精度数组

[英]Promise.all returns double array

In my node Router I have the following: 在我的节点路由器中,我具有以下内容:

router.get("/single-base/:id", (req, res) => {
Base.find({ _id: req.params.id })
    .then(bases => {
        let basefetches = [];

        for (let base of bases) {
            basefetches.push(BaseUser.find({ baseId: req.params.id }));
        }
        return Promise.all(basefetches).then(users =>
            res.json(users.map(user => user.serialize()))
        );
    })
    .catch(err => {
        console.error(err);
        res.status(500).json({ message: "Internal server error" });
    });
});

when console logging return Promise.all(basefetches).then(users => console.log(users)) I receive: 当控制台日志记录return Promise.all(basefetches).then(users => console.log(users))我收到:

[ [ { created: 2018-08-27T21:37:42.151Z,
  _id: 5b846f268cb4481264298f82,
  userId: 'tester2',
  baseId: 5b81528e1314da25bc498085,
  acceptedMembership: false,
  isCreator: false,
  __v: 0 } ] ]

I have the exact same method in another place, and it returns only an empty array (this array should be empty in the beginning as well). 我在另一个地方有完全相同的方法,它只返回一个空数组(该数组开头也应该为空)。 Why/when/how is this pushing an array into another array, how do I solve this so that the users.map does not throw: 为什么/何时/如何将一个数组推入另一个数组,如何解决这个问题,以便users.map不会抛出:

TypeError: Cannot read property 'push' of undefined
at Base.find.then.bases

EDIT : I believe this has to do with the BaseUser.find({}) method. 编辑 :我相信这与BaseUser.find({})方法有关。 When console logging: Promise.all(basefetches).then(users => console.log(users)); 控制台记录时: Promise.all(basefetches).then(users => console.log(users)); I get the following: 我得到以下内容:

{ created: 2018-08-25T12:43:02.640Z,
  _id: 5b81528e1314da25bc498085,
  creatorId: 5b7efd4db3036a13601ad8d7,
  title: 'testBase1',
  __v: 0 }
[ [ { created: 2018-08-27T21:37:42.151Z,
      _id: 5b846f268cb4481264298f82,
      userId: 'tester2',
      baseId: 5b81528e1314da25bc498085,
      acceptedMembership: false,
      isCreator: false,
      __v: 0 } ] ]

I 一世

Looks like MongoDB data. 看起来像MongoDB数据。 And if BaseUser.find() uses MongoDB's db.collection.find() , then it returns a cursor (hence the conversion to array at this level I guess). 而且,如果BaseUser.find()使用MongoDB的 db.collection.find() ,那么它将返回一个游标(因此,我认为此级别的转换为数组)。

If the baseId is intended to be unique (ie you expect one result at the most), you might want to use db.collection.findOne() instead. 如果baseId打算是唯一的(即,您最多期望一个结果),则可能要使用db.collection.findOne()

However, I don't understand why you wrap 但是,我不明白你为什么要包扎

basefetches.push(BaseUser.find({ baseId: req.params.id }));

in a loop over bases , since bases is not used at all in the loop. 在遍历bases ,由于bases没有在循环使用的。 Seems like you just repeat the exact same query yielding the exact same result for as many bases as you got. 似乎您只是重复进行完全相同的查询,从而为您获得的所有bases产生了完全相同的结果。

Anyway: one array because of find() , and one because of the promises-slash-loop-over- bases . 无论如何:一个数组是因为find() ,另一个是由于promises-slash-loop-over- bases

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

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