You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I was writing API for finding all answered question by user who is logged in.
QuestionAnswerSchema -
var questionSchema = new mongoose.Schema({ question : { type : String, unique : true, required : true }, answers : [{ answer : { type: String, }, upvote : { type : Number }, downvote : { type : Number }, views : { type : Number }, author : { type : String }, comments : [{ comment : { type : String }, author : { type : String } }] }], author : { type : String, required: true }, tag : { type : String, required : true, default : 'Other' }, totalviews : { type : Number, default: 0 }, totallikes : { type : Number, default : 0 } });
Here if a user answers a question his username is saved in answers -> author in questionSchema. Now if i want to find out all answer by a user i have to run two loops as below -
` for(var i=0;i < questions.length;i++) {
if(questions[i].answers.length > 0) {
for(var j=0;j<questions[i].answers.length;j++) {
if(questions[i].answers[j].author === req.params.username) {
answeredArrayObj.question = questions[i].question;
answeredArrayObj.answer = questions[i].answers[j].answer;
answeredArrayObj.tag = questions[i].tag;
answeredArrayObj._id = questions[i]._id;
answeredArray.push(answeredArrayObj);
// again make it empty for next answer
answeredArrayObj = {};
}
}
}
}`
It is too much time consuming.
How to contribute -
Fork repo.
Play with code and make suitable changes
Make a PR
The text was updated successfully, but these errors were encountered:
Hey ,
I was writing API for finding all answered question by user who is logged in.
QuestionAnswerSchema -
var questionSchema = new mongoose.Schema({ question : { type : String, unique : true, required : true }, answers : [{ answer : { type: String, }, upvote : { type : Number }, downvote : { type : Number }, views : { type : Number }, author : { type : String }, comments : [{ comment : { type : String }, author : { type : String } }] }], author : { type : String, required: true }, tag : { type : String, required : true, default : 'Other' }, totalviews : { type : Number, default: 0 }, totallikes : { type : Number, default : 0 } });
Here if a user answers a question his username is saved in answers -> author in questionSchema. Now if i want to find out all answer by a user i have to run two loops as below -
` for(var i=0;i < questions.length;i++) {
It is too much time consuming.
How to contribute -
The text was updated successfully, but these errors were encountered: