JavaScript代码不按照想要的顺序运行(Node.js,MongoDB)

我知道Node.js的非阻塞I / O和什么是asynchronous函数,但是我很难指出为什么这个代码像运行一样运行。

我连接到一个MongoDB集合,search重复,把第一个值重复自己的数组对象(dupIndex)。 当数组打印时( console.log(dupIndex); ),我看到2个值,但是当我稍后使用.length属性( console.log(dupIndex.length); )时为0。

我想用dupIndex中的数据继续操作集合(就像使用deleteMany方法), 但是如果它显示0,我不能,至less不会这样。 有人可以解释这一点,并帮助我呢?

谢谢!

//connect us to a running MongoDB server const {MongoClient, ObjectID} = require('mongodb'); var dataBase = "TodoApp"; //connecting to a database //connects to the database /TodoApp, if no such db exists it auto creates it MongoClient.connect(`mongodb://localhost:27017/${dataBase}`, (err, db)=>{ if(err){ //return or 'else' - so if an error happens, it won't continue return console.log(`Unable to connect. Error: ${err}`); } console.log(`Connected to MongoDB server. Database: ${dataBase}.`); var dupIndex = []; //find all db.collection('Todos') .find() .toArray().then((docs) => { for ( var i =0; i< docs.length -1; i++){ for(var j =i+1; j< docs.length; j++){ if(docs[i].text === docs[j].text) { console.log(`in`); dupIndex.push(docs[i].text); i++; } } } console.log(dupIndex); }, (err)=> { console.log(`unable to fetch`); }); console.log(dupIndex.length); // for(var i = 0; dupIndex) //close the connection to the db db.close(); }); 

因为console.log(dupIndex.length); 在嵌套循环之前运行。

 db.collection('Todos') .find() .toArray() 

这是一个asynchronous调用,控制权传递给console.log(dupIndex.length); 尝试编写console.log(dupIndex.length); 在console.log旁边(dupIndex);

例如:

  db.collection('Todos') .find() .toArray().then((docs) => { for ( var i =0; i< docs.length -1; i++){ for(var j =i+1; j< docs.length; j++){ if(docs[i].text === docs[j].text) { dupIndex.push(docs[i].text); i++; } } } return dupIndex; }, (dupIndexRecieved)=> { console.log(dupIndexRecieved.length); data recieved here }, (err)=> { console.log(`unable to fetch`); }); 

//将我们连接到正在运行的MongoDB服务器

 const {MongoClient, ObjectID} = require('mongodb'); var dataBase = "TodoApp"; //connecting to a database //connects to the database /TodoApp, if no such db exists it auto creates it MongoClient.connect(`mongodb://localhost:27017/${dataBase}`, (err, db)=>{ if(err){ //return or 'else' - so if an error happens, it won't continue return console.log(`Unable to connect. Error: ${err}`); } console.log(`Connected to MongoDB server. Database: ${dataBase}.`); var dupIndex = []; //find all db.collection('Todos') .find() .toArray().then((docs) => { for ( var i =0; i< docs.length -1; i++){ for(var j =i+1; j< docs.length; j++){ if(docs[i].text === docs[j].text) { console.log(`in`); dupIndex.push(docs[i].text); i++; } } } //Mongo db find returns a promise. The value you are accessing with the then function. So whatever you want to do with the db query return value you have to do it here inside the then function. console.log(dupIndex.length); console.log(dupIndex); // for(var i = 0; dupIndex) //close the connection to the db db.close(); }, (err)=> { console.log(`unable to fetch`); }); //when you make the call here its get called before the then function. Thats why the length is zero. });