Javascript 中的提升/返回变量
Hoisting / Returning variable in Javascript
我有以下代码来查询 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);
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
我想使用存储在变量文档中的查询结果进行进一步处理。但是它们不会从函数中返回。即表达式
findDocuments(db, function() {
console.log(docs);
client.close();
});
我收到 "undefined" 返回。我做错了什么?
您需要按如下方式更新 findDocuments 函数调用,
findDocuments(db, function(docs) {
console.log(docs);
client.close();
});
您不需要顶部的 docs
变量。使用局部变量如下,
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
语句,因为它与回调一起没有任何重要性。
最后,我建议你多了解一下回调(最好是 promises)
改变这个
function() {
console.log(docs);
client.close();
});
对此
function(docs) {
console.log(docs);
client.close();
});
因为在您的代码中,您在代码顶部记录了 docs 变量,该变量未收到任何值尝试新代码并告诉我。现在有用吗?我想是的。
我有以下代码来查询 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);
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
我想使用存储在变量文档中的查询结果进行进一步处理。但是它们不会从函数中返回。即表达式
findDocuments(db, function() {
console.log(docs);
client.close();
});
我收到 "undefined" 返回。我做错了什么?
您需要按如下方式更新 findDocuments 函数调用,
findDocuments(db, function(docs) {
console.log(docs);
client.close();
});
您不需要顶部的 docs
变量。使用局部变量如下,
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
语句,因为它与回调一起没有任何重要性。
最后,我建议你多了解一下回调(最好是 promises)
改变这个
function() {
console.log(docs);
client.close();
});
对此
function(docs) {
console.log(docs);
client.close();
}); 因为在您的代码中,您在代码顶部记录了 docs 变量,该变量未收到任何值尝试新代码并告诉我。现在有用吗?我想是的。