MongoDB和Node js中的未定义结果

编程入门 行业动态 更新时间:2024-10-09 14:26:56

MongoDB和Node js中的<a href=https://www.elefans.com/category/jswz/34/1771258.html style=未定义结果"/>

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中的未定义结果

本文发布于:2024-05-07 06:28:17,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1754815.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:未定义   MongoDB   Node   js

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!