简体   繁体   中英

Querying embedded mongodb with node js

I try to fetch a value from my age collection and check by applying the condition if(age===5) then it will fetch data again from other collection question and send the data to the browser. But it will show me the error

Error While Saving the result TypeError: Cannot read property 'age_group_5' of undefined

Code:

1). Node js

 var agegroupQuiz = require('../models/agegroupSchema.js');
 var questionQuiz = require('../models/question.js');

 exports.agegroupController = function(req,res,next){

    try{
    //var age=5;   
    var queryObj = {};
    var projection1 = '-_id, age_group.age_group_5.max_age';
    var projection2 = '-_id question';


        //agegropup Schema
       var a = agegroupQuiz.findOne(queryObj,projection1); 
       console.log(a.age_group.age_group_5.max_age);
       var age = a.age_group.age_group_5.max_age;


       if(age===5){

        //question Schema
        questionQuiz.find(queryObj,projection2,function(err, data){
            if(err){
             console.log('getQuestions : Error while getting Questions ' + err);
             return next(err);
            }
             //console.log(question);
             res.send(data);
        });


       }else{console.log("error");}

    }catch(err){
        console.log('Error While Saving the reuslt ' +err);
        return next(err);
    }

   }


 /* exports.agemethod = function(req, res, next){
  }*/

2). Mongodb Schema

 a). var mongoose = require('mongoose');

    module.exports = (function question () {

    var Schema = mongoose.Schema;

    var question = new Schema({

    question:{type:Array,

    _id:{type:Number},
    title:{type:String},
    options:{type:Array},
    result:{type:Array},
    feedback:{type:String}
    },
    metadata:{
        type:String,
        category:String,
        age_group:String,
        location:String
    }

 });

  var results = mongoose.model('userquestion', question);

  return results;

 })();

 b). var mongoose = require('mongoose');

    module.exports = (function agegroup () {

   var Schema = mongoose.Schema;

   var agegroup = new Schema({

   age_group : {

    age_group_5: {

        _id:{type:String},
        max_age:{type:Number}
    }
   }
   });

   var results = mongoose.model('age', agegroup);

   return results;

  })();

I would recommend you try this code and hopefully it should work.

 var agegroupQuiz = require('../models/agegroupSchema.js');
 var questionQuiz = require('../models/question.js');


 exports.agegroupController = function(req,res,next){


try{
//var age=5;   
var queryObj1 = {};  //for max age 5
var queryObj2 = {};  //for question

queryObj1 = {
      _id: //id,
      maxAge: 5   //please put the key in accordance to your schema
};

queryObj2 = {
      _id: //id,
      question: //question
}

    //agegropup Schema
   return agegroupQuiz.findOne(queryObj1)
   .then(function(maxAge){
    var age = maxAge;

    if(age == 5){
          questionQuiz.find(queryObj,projection2,function(err, data){
               if(err){
                    console.log('getQuestions : Error while getting Questions ' + err); 
          }
         //console.log(question);
         res.send(data);
       });
    }else{
          console.log("Error");
    }

   }).catch(function(error){
         console.log('Error While Saving the reuslt ' +err);
   });

  }
  }

I may have missed some curly braces, please verify that, and ignore the indentation.

make sure you use the correct keys of your model.

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM