Javascript中的提升/返回变量
我有以下代码来查询MongoDB数据库:Javascript中的提升/返回变量,javascript,node.js,mongodb,return,hoisting,Javascript,Node.js,Mongodb,Return,Hoisting,我有以下代码来查询MongoDB数据库: var docs; // Use connect method to connect to the server MongoClient.connect(url, function(err, client) { assert.equal(null, err); console.log("Connected successfully to server"); const db = client.db(dbName); findD
var docs;
// Use connect method to connect to the server
MongoClient.connect(url, function(err, client) {
assert.equal(null, err);
console.log("Connected successfully to server");
const db = client.db(dbName);
findDocuments(db, function() {
console.log(docs);
client.close();
});
});
const findDocuments = function(db, callback) {
// Get the documents collection
const collection = db.collection('oee');
// Find some documents
collection.find(query).toArray(function(err, docs) {
assert.equal(err, null);
console.log("Found the following records");
//console.log(docs);
callback(docs);
return docs;
});
};
}
哪些产出:
Connected successfully to server
Found the following records
undefined
我想使用存储在变量文档中的查询结果进行进一步处理。但是,它们不会从函数返回。i、 e.表达方式
findDocuments(db, function() {
console.log(docs);
client.close();
});
我得到一个“未定义”的返回。我做错了什么?您需要更新findDocuments函数调用,如下所示
findDocuments(db, function(docs) {
console.log(docs);
client.close();
});
const findDocuments = function(db, callback) {
// Get the documents collection
const collection = db.collection('oee');
// Find some documents
collection.find(query).toArray(function(err, docs) {
assert.equal(err, null);
console.log("Found the following records");
return callback(docs);
});
}
顶部不需要docs
变量。按如下方式使用局部变量:
findDocuments(db, function(docs) {
console.log(docs);
client.close();
});
const findDocuments = function(db, callback) {
// Get the documents collection
const collection = db.collection('oee');
// Find some documents
collection.find(query).toArray(function(err, docs) {
assert.equal(err, null);
console.log("Found the following records");
return callback(docs);
});
}
还要注意,我删除了return docs
语句,因为它与回调没有任何关系
最后,我建议您进一步了解回调(最好是承诺)您需要更新findDocuments函数调用,如下所示:
findDocuments(db, function(docs) {
console.log(docs);
client.close();
});
const findDocuments = function(db, callback) {
// Get the documents collection
const collection = db.collection('oee');
// Find some documents
collection.find(query).toArray(function(err, docs) {
assert.equal(err, null);
console.log("Found the following records");
return callback(docs);
});
}
顶部不需要docs
变量。按如下方式使用局部变量:
findDocuments(db, function(docs) {
console.log(docs);
client.close();
});
const findDocuments = function(db, callback) {
// Get the documents collection
const collection = db.collection('oee');
// Find some documents
collection.find(query).toArray(function(err, docs) {
assert.equal(err, null);
console.log("Found the following records");
return callback(docs);
});
}
还要注意,我删除了return docs
语句,因为它与回调没有任何关系
最后,我建议您进一步了解回调(最好是承诺)改变这一点
function(){
console.log(文档);
client.close();
});代码>
对此
}));
因为在您的代码中,您将文档变量记录在未收到任何值的代码顶部,请尝试新代码并告诉我。现在能用了吗?我想是的。改变这个
function(){
console.log(文档);
client.close();
});代码>
对此
}));
因为在您的代码中,您将文档变量记录在未收到任何值的代码顶部,请尝试新代码并告诉我。现在能用了吗?我想是的。非常感谢!这两个答案都是正确的,而且都有效。不客气。你能投票吗?非常感谢!这两个答案都是正确的并且有效?欢迎您投票。谢谢,对更好地理解callabacks非常有帮助。我被嵌套函数弄糊涂了。在MongoClient.connect函数作用域之外,需要什么才能使“docs”可用?为此,可以使用全局变量(例如var docs\u global
)。然后您可以将docs
的值分配给docs\u global
。但我不推荐这种做法。如果您需要保持全局状态,我建议您使用新的ES6类SyntaxThank,这对更好地理解callabacks非常有帮助。我被嵌套函数弄糊涂了。在MongoClient.connect函数作用域之外,需要什么才能使“docs”可用?为此,可以使用全局变量(例如var docs\u global
)。然后您可以将docs
的值分配给docs\u global
。但我不推荐这种做法。如果需要维护全局状态,我建议使用新的ES6类语法