0

all I have to try to print a number in console, that is saved in my MongoDB but when I am fetching this as below data.min_score it gives me undefined in my console. I have also used parseInt then output comes NaN

according to my scheme, my min_score value is 10 my requirement is to print 10 in console or browser

1). Node js Code

   this.levelChange = function(req, res, next){

    try{

        var arr = [];
        var query = {level_num:1};
        QuizLevel.find(query,function(err,data){

            arr.push(data.min_score);
            console.log(arr);
        });
    }catch(err){
        console.log("Error");
        return next(err);
    }
};

2).Level Schema

   var LevelSchema = new Schema([
     {
     _id:{type:String},
     age:{type:Number},
     level_num:{type:Number},
     min_score:{type:Number},
     max_questions:{type:Number}
     }

3). console output

  undefined

4). This is my Json data

   {
   "age":5,
   "level_num":1,
   "min_score":10,
   "max_questions":30
   }
   {
    "age":5,
    "level_num":2,
    "min_score":12,
    "max_questions":33
   }
   {
   "age":5,
   "level_num":3,
   "min_score":15,
   "max_questions":35
   }
Ritesh Kumar
  • 79
  • 1
  • 2
  • 9
  • Possible duplicate of [Mongo shell execute query from file and show result](http://stackoverflow.com/questions/16326241/mongo-shell-execute-query-from-file-and-show-result) – apieceofbart Aug 21 '16 at 10:26
  • 2
    Hi Ritesh, welcome to [so]. If you're editing your post, please make sure to use the same account that you used to make it. If you cannot access your account, see http://stackoverflow.com/help/reset-password – Qantas 94 Heavy Aug 21 '16 at 12:58

1 Answers1

0

When you use .find function for a schema, you return an array of objects that match the query given in the parameter. Here, you're using -
arr.push(data.min_score);
But data is an array. Try using
arr.push(data[0].min_score);
See if it makes a difference and comment on this.

Zeokav
  • 1,653
  • 4
  • 14
  • 29
  • thanks for reply sir it works but half. now current output comes 1 on a console. Also, I am sending my level JSON data. – Ritesh Kumar Aug 21 '16 at 10:39
  • 1
    Break it down for me. In the code you've posted, you're querying the database for records that have level_num 1, right? That means it should return only one document, that is the record with min_score as 10. I'm not sure if this is the error here, but make your query object this - `var query = {'level_num':1}` (attribute in quotes). If that doesn't work, do a console.log(data) and post the result here. – Zeokav Aug 21 '16 at 11:02