【问题标题】:Objects not being passed correctly对象未正确传递
【发布时间】:2013-08-18 04:24:33
【问题描述】:

正在调用一个瀑布函数,其中一个内部函数获取一组用户文档并尝试查找它们是用户本人还是用户的朋友。基于这两件事,我想返回每个用户文档,“我”是真/假,“朋友”是文档/假。文档本身不是真的。但是,我得到的结果是不正确的。

代码如下:

function(users, callback) {
   async.mapSeries([users], function(user, next) {
        Friend.findOne({userId: req.signedCookies.userid, friend_id: user}, function(err, friend) {
            var me = false;
            if (user.id === req.signedCookies.userid) {
                console.log('me check');
                me = true;
            }
            if (friend === undefined) {
                friend = false;
            }
            var object = {'user': user, 'friend': friend, 'me': me};
            //searchResults.push(object);
            next(err, object);
        });
    }, function(err, searchResults) {
        console.log(searchResults);
        callback(null, searchResults);
    });
}

这是我得到的结果:

[ { user:
     [ { firstName: 'test1',
         lastName: 'test1s',
         email: 'test1@gmail.com',
         emailTrue: 'test1@gmail.com',
         firstNameTrue: 'Test1',
         lastNameTrue: 'teST1s',
         password: '$2a$10$irIz5rVSFwGFVjzXqAqdxuxa8wAFoV99FulXykt
',
         phone: 2738483282,
         birthday: Tue Aug 22 823 20:00:00 GMT-0400 (Eastern Dayli
         email_confirmed: true,
         date_created: Thu Aug 15 2013 14:54:17 GMT-0400 (Eastern
,
         _id: 520d23d9367604cc0d000001,
         __v: 0,
         socialAccounts: [],
         phoneList: [],
         emailList: [] },
       { firstName: 'test1',
         lastName: 'test1ss',
         email: 'test1s@gmail.com',
         emailTrue: 'test1s@gmail.com',
         firstNameTrue: 'test1',
         lastNameTrue: 'test1ss',
         password: '$2a$10$kaWYHkg68c4Uti6/DlNVR.N0Ojzo.RnsQ6BARTd
',
         phone: 888439348,
         birthday: Mon Aug 30 94 20:00:00 GMT-0400 (Eastern Daylig
         email_confirmed: true,
         date_created: Thu Aug 15 2013 15:11:19 GMT-0400 (Eastern
,
         _id: 520d27d7a767bdbc1c000001,
         __v: 0,
         socialAccounts: [],
         phoneList: [],
         emailList: [] } ],
    friend: false,
    me: false } ]

【问题讨论】:

  • 你为什么将[users] 传递给async.mapSeries 而不仅仅是users

标签: node.js mongodb asynchronous mongoose


【解决方案1】:

users 直接传递到您的async.mapSeries 调用中,而不是将其包装在数组括号中:

async.mapSeries(users, function(user, next) {

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-10-18
    • 2016-04-23
    • 1970-01-01
    • 1970-01-01
    • 2016-05-23
    • 2018-02-14
    • 2014-10-31
    相关资源
    最近更新 更多