简体   繁体   English

node.js无法从mongodb地图集控制台日志数据

[英]nodejs could not console log data from mongodb atlas

nodejs connects to atlas succesfully but when i try to console log the data I get error nodejs成功连接到地图集,但是当我尝试控制台记录数据时,出现错误

const uri = "mongodb+srv://daww:pass@cluster0-shard-00-00-fmmcx.mongodb.net/admin"

MongoClient.connect(uri, function(err, client) {
   if(err) {
        console.log('Error occurred while connecting to MongoDB Atlas...\n',err);
   }
   console.log('Connected...');
   const collection = client.db("star").collection("clc").find({}).toArray(function(err,result){

    if(err) throw err;

    console.log(result)

   });

   client.close();
});

I was expecting a json object with all the content of the database however, I am getting the following error 我本来期望一个json对象具有数据库的所有内容,但是,出现以下错误

MongoNetworkError: connection destroyed, not possible to instantiate cursor

You are using an asynchronous method find to retrieve data. 您正在使用异步方法find来检索数据。 The data will only get logged when the method finishes executing and the callback function gets executed. 仅在该方法完成执行并且执行回调函数时才记录数据。 Meanwhile, the last client.close() statement will close the connection before your data gets retrieved. 同时,最后的client.close()语句将在检索数据之前关闭连接。

Try commenting out the close statement or moving it inside your method callback. 尝试注释掉close语句或将其移入方法回调中。

检查您的IP地址是否在MongoDB Atlas群集>安全选项卡 下列出白色

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM