用节点js查询嵌入式mongodb

时间:2016-07-21 10:06:39

标签: javascript node.js mongodb

我尝试从我的年龄集合中获取值并通过应用条件if(age===5)进行检查,然后它将从其他集合问题中再次获取数据并将数据发送到浏览器。但它会告诉我错误

  

保存结果时出错TypeError:无法读取未定义

的属性'age_group_5'

代码:

1)。节点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;

  })();

1 个答案:

答案 0 :(得分:0)

我建议您尝试使用此代码,并希望它能够正常运行。

 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);
   });

  }
  }

我可能错过了一些花括号,请验证,并忽略缩进。

确保使用模型的正确键。