first of all, the models of my mongoose is as follows:
var clubsSchema = new mongoose.Schema({
"clubCreatetime": String,
"clubCreater": String,
"clubName": String,
"clubDescription": String,
"clubImage": String,
"clubMenbers": [String],
"clubTopics": [{
"topicCreatetime": String,
"topicCreater": String,
"topicTitle": String,
"topicContent": String,
"topicComment": [{
"userId": String,
"userName": String,
"commentCreatetime": String,
"commentContent": String
}],
"topicLike": [String],
"topicState": String
}],
"clubState": String
})
then, my code is as follows:
let orgId = req.param("orgId");
let topicId = req.param("topicId");
Club.findOne({
_id: orgId
}, function(clubErr, clubDoc){
//console.log(clubDoc);
if(clubErr){
res.json({
status: "1",
msg: clubErr.message,
result: ""
})
} else {
clubDoc.clubTopics.findOne({
_id: topicId
}, function(topicErr, topicDoc){
console.log(topicDoc);
if(topicErr){
res.json({
status: "1",
msg: topicErr.message,
result: ""
})
} else {
res.json({
status:"0",
msg: "",
result: topicDoc
})
}
})
}
})
here, I want to find the community first through the community Id, in the route, and then query the entire content of the topic according to the topic Id, in the route. However, it reminds me that the method .findOne () does not exist, so I would like to ask for advice, I want to query this topic under the community according to id, how to write it? Thank you!
the current way to return topic information is:
for(let i=0; i<clubDoc.clubTopics.length; iPP){
if(clubDoc.clubTopics[i]._id==topicId){
console.log(clubDoc.clubTopics[i]);
}
}
would you like to ask mongoose if there is a more direct way?