不能使用mongoose的响应对象

时间:2016-05-31 08:07:53

标签: node.js mongodb mongoose

我被一些非常愚蠢的东西困住了,这让我疯狂。

问题:

function getPostsCount(callback) {
        userPostsModel.findOne({'profileID':req.session.facebookProfileId}, function(err, usersPostCountDB) {
            if(usersPostCountDB) {
                console.log('usersPostCountDB found and the userPost var passed with it is: ' + usersPostCountDB.postsCount);
                console.log(JSON.stringify(usersPostCountDB));
                callback(null, usersPostCountDB.postsCount);
            } else {
                console.log('There was an error getting the postsCount');
                callback(true, null);
            }
        });
    }

这是奇怪的部分,我得到以下内容:

usersPostCountDB found and the userPost var passed with it is: undefined

此外,我已经查看了下一行的响应,我正在获取对象,并且它的postsCount变量正确。它看起来像这样:

enter image description here

我的架构如下所示:

var Schema = mongoose.Schema;

var userSchema = new Schema({
   profileID:       String,
   fullname:        String,
   profilePic:      String,
   email:           String,
   birthday:        String,
   location:        String,
   about:           String,
   likes:           [],
   interests:       [],
   postsCount:      String,
   posts:           [],
   postsAgreed:     [],
   postsDisagreed:  [],
   dateCreated:     String
});
mongoose.model('user', userSchema, 'users');

任何想法都有?我现在真的需要帮助。

谢谢, 砂眼

1 个答案:

答案 0 :(得分:0)

是否有断点(不知道为什么我没有想到这一点)并发现usersPostCountDB对象有一个_doc对象,其中包含所有变量。

所以为了得到它,我只是使用了:

usersPostCountDB._doc.postsCount

而不是:

usersPostCountDB.postsCount