开发者问题收集

如何通过 Mongoose 从 Mongodb 中的嵌套文档中获取 Nodejs 中的数组?

2019-06-12
115

我试图从 mongoose Model 对象中以数组的形式获取电子邮件。因此,我可以在查询中循环数组并按时间排序获取其他用户的所有帖子,我是 Node JS 和 Javascript 的新手。以下是代码和架构:

这是架构:

var userSchema = mongoose.Schema({

_id:                {type: String, default: mongoose.Types.ObjectId()},
email:              {type: String},
userProfile:       userProfileSchema
});

var userProfileSchema = mongoose.Schema({

friendNetwork:      [{

"email": String, 
"chatId": String, 
"name": String

}],

posts:              [{
                        "_id" : {type: String, default: mongoose.Types.ObjectId()},
                        "postCreator" : String,
                        "isShared" : {type: String, default: ""},
                        "timestamp": String,
                        "text": String,
                        "images": [String],
                        "videos": [String],
                        "likes": [String],
                        "comments": [{

                            "status": {type: Boolean, default: true},
                            "commentId" : {type: String, default: mongoose.Types.ObjectId()},
                            "commentCreator": String,
                            "comment": String,
                            "timestamp": String,

                        }],
                        "shares": [String],
                        "isDelete" : {Boolean, default: false},
                        "isOnlyMe": {Boolean, default: false}

                    }],

isDelete:           {type: Boolean, default: false}
});

我尝试了 map 函数,但出现错误“TypeError:无法读取 null 的属性‘map’”。

这是代码:

app.get('/newsfeed/getfeed', (req, res, err) => {

try
{
    // active user email in query
    userModel.findOne({email: req.query.email}, (user) => { 

    // array to get email of all active user's friends
    let userEmails = user.map(user => user.userProfile.friendNetwork.email);       

    //traverse through all friends and get their respective posts shared with friends
    userModel.find({email : { $in: userEmails }}, (err,users) => {

            users.userProfile.posts.find({ isOnlyMe: false }, (posts) => {
                // Mongo Atlas already ordered by timestamp, no need to add further sorting
                res.send(posts);
            });
        });                    
    });  
}
catch 
{
    console.log(err);
    logger(err, "Getting newsfeed of main page failed.");
}  

});

2个回答

回调中提供的第一个参数通常是错误值。您应该将代码行更改为:

userModel.findOne({email: req.query.email}, (err, user) => {

您可以在此处查看该函数的文档: https://mongoosejs.com/docs/api.html#model_Model.findOne

MattB
2019-06-12

找到了解决方案,显然我没有将数组传递给 map 函数。map 函数仅接受并通过数组工作。

app.get('/newsfeed/getfeed', (req, res, err) => {

try
{
    // active user email in query
    userModel.findOne({email: req.query.email}, (err,user) => { 

    // array to get email of all active user's friends
    ***let userEmails = user.userProfile.friendNetwork.map((friend) =>{***
        return friend.email;
    });       

    userEmails.push(req.query.email);

    console.log(userEmails);        
    //traverse through all friends and get their respective posts shared with friends
    userModel.find({ email: { $in: userEmails }}, (err,users) => {

            console.log(users);
            users.userProfile.posts.find({ isOnlyMe: false }, (err,posts) => {
                // Mongo Atlas already ordered by timestamp, no need to add further sorting
                res.send(posts);
            });
        });                    
    });  
}
catch 
{
    console.log(err);
    logger(err, "Getting newsfeed of main page failed.");
}
});
Shahzar Khalid
2019-06-12