I have to try to print the value of my MongoDB in the console. But it gives me undefined in my console I had done a lot of things to get the result but all is vain I want to print number single field value (min_score) in console or browser from MongoDB using node.js code
1). This is my node.js code
this.levelChange = function(req, res, next){
try{
var arr = [];
var query = {'level_num':1};
QuizLevel.find(query,function(err,data){
var a = parseInt(arr.push(data));
console.log(a.min_score);
});
}catch(err){
console.log("Error");
return next(err);
}
};
return this;
2). This is my js level schema
{
_id:{type:String},
age:{type:Number},
level_num:{type:Number},
min_score:{type:Number},
max_questions:{type:Number}
}
3). This is my json value
{
"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
}
4). This is the result in console
[undefind]
I want to get the result min_score 10 in my console
if data is the output like this:
{
"age":5,
"level_num":1,
"min_score":10,
"max_questions":30
}
Then you can simply use data.min_score and output will be: 10
var a = parseInt(arr.push(data)); will gives you the a as 1 so a.min_score will be undefined definitely.
I faced the same problem. Instead of var a = parseInt(arr.push(data));
You need to destructure the array and then use data.min_score
QuizLevel.find(query,function(err,data){
const [a] = data
console.log(a.min_score);
})
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.