未定义结果"/>
MongoDB和Node js中的未定义结果
我想获取我的集合数据,我的集合名为students
,并且是我的pool
数据库的一部分。
与mongoDB的连接有效,但是console.log(result.lastname)
返回undefined
。
这是我的server.js
文件
var mongo = require('mongodb');
var assert = require('assert');
const url = 'mongodb://localhost:27017/';
mongo.connect(url, function (err, db) {
if(err) {
console.log("Connection failed");
}
console.log("Connection successfull");
var dbo = db.db("pool");
dbo.collection("students").find({}, function(err, result) {
if (err) throw err;
console.log(result.lastname);
db.close();
});
});
以及我直接在控制台中使用students
看到的db.students.find();
集合中的内容
{ "_id" : ObjectId("5eb1570f2c0167c90cc127fd"), "id" : 0, "lastname" : "Sam", "firstname" : "Sung", "email" : "[email protected]", "phone" : 613295990, "validated" : "Validated", "admin" : true }
回答如下:我认为它正在返回一个数组,可以尝试:
result[0].lastname
更多推荐
MongoDB和Node js中的未定义结果
发布评论