最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

MongoDB和Node js中的未定义结果

网站源码admin25浏览0评论

MongoDB和Node js中的未定义结果

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
发布评论

评论列表(0)

  1. 暂无评论