I'm trying to get just the emails in the form of an array, from a mongoose Model object. So I can loop the array in a query and get all the posts from the other users in a time sorted order, I'm new to node JS and Javascript. Here's the code, and the schema:
This is the schema:
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}
});
I tried the map function but, I get the error "TypeError: Cannot read property 'map' of null".
This is the code:
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.");
}
});
The first parameter provided in a callback is typically an error value. You should change the line of code to:
userModel.findOne({email: req.query.email}, (err, user) => {
You can view the documentation for that function here: https://mongoosejs.com/docs/api.html#model_Model.findOne
Found the solution, apparently I wasn't passing an array to the map function. map function only accepts and works through an array.
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.");
}
});
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.