Node.js NodeJS+;MongoJS:嵌套回调问题
我还是一个NodeJS的n00b,所以这个问题可能有点初级 我正在使用MongoJS阅读两个逻辑相关的集合。第一个find()返回一个值,我将该值传递给第二个find()以获取所需信息 我尝试了几种策略,最后一种(snippet#1)是我导出的类 在此之前,我只是有一个函数,它返回所需的值,即“config[0]” 在这段代码中,我所做的只是将“sapConfig”属性设置为单词“test”,但当我执行这段代码时,在调用“get_config()”方法之后,“sapConfig”的值总是“null”,最奇怪的是,对“this.sapConfig='test'”的引用会生成一个错误,即“无法设置未定义的属性'sapConfig' 当我将代码作为带有return语句(snippet#2)的简单函数时,没有生成错误,但返回的值始终是“未定义的”,尽管console.log()语句显示返回的变量的值具有所需的值。有什么好处 代码片段#1:返回对象Node.js NodeJS+;MongoJS:嵌套回调问题,node.js,mongojs,Node.js,Mongojs,我还是一个NodeJS的n00b,所以这个问题可能有点初级 我正在使用MongoJS阅读两个逻辑相关的集合。第一个find()返回一个值,我将该值传递给第二个find()以获取所需信息 我尝试了几种策略,最后一种(snippet#1)是我导出的类 在此之前,我只是有一个函数,它返回所需的值,即“config[0]” 在这段代码中,我所做的只是将“sapConfig”属性设置为单词“test”,但当我执行这段代码时,在调用“get_config()”方法之后,“sapConfig”的值总是“nul
"use strict";
var mongojs = require('mongojs'); // MongoDB API wrapper
module.exports = function(regKey) {
this.regKey = regKey;
this.sapConfig = null;
this.get_config = function() {
// Read SAP connection information from our MONGO db
var db = mongojs('mongodb://localhost/MIM', ['Configurations','Registrations']);
db.Registrations.find({ key: this.regKey }, function(err1, registration){
console.log('Reg.find()');
console.log(registration[0]);
db.Configurations.find({ type: registration[0].type }, function(err2, config){
console.log('Config.find()');
console.log('config=' + config[0].user);
this.sapConfig = 'test';
});
});
}
this.get_result = function() {
return this.sapConfig;
}
}
同样,代码段#1中的代码在调用“get_config()”时,在执行行“this.sapConfig='test'时导致错误
但是,在这个错误之后,我可以执行“obj.get_result()”,并得到初始化它的值,即null。换句话说,相同的代码不会生成错误,说明“this”未定义为“get_config()”方法中的
代码片段#2:使用“return”语句
"use strict";
var mongojs = require('mongojs'); // MongoDB API wrapper
module.exports = function(regKey) {
// Read SAP connection information from our MONGO db
var db = mongojs('mongodb://localhost/MIM', ['Configurations','Registrations']);
db.Registrations.find({ key: regKey }, function(err1, registration){
console.log('Reg.find()');
console.log(registration[0]);
db.Configurations.find({ type: registration[0].type }, function(err2, config){
console.log('Config.find()');
console.log('config=' + config[0].user);
return config[0].user;
});
});
}
当我收到返回值并检查它时,它是“未定义的”。例如,在节点CL I上发出以下命令:
var config = require('./config') // The name of the module above
> var k = config('2eac44bc-232d-4667-bd24-18e71879f18c')
undefined <-- this is from MongoJS; it's fine
> Reg.find() <-- debug statement in my function
{ _id: 589e2bf64b0e89f233da8fbb,
key: '2eac44bc-232d-4667-bd24-18e71879f18c',
type: 'TEST' }
Config.find()
config=MST0025
> k <-- this should have the value of "config[0]"
undefined
var config=require('./config')//上面模块的名称
>变量k=config('2eac44bc-232d-4667-bd24-18e71879f18c')
未定义的无法访问此.sapConfig
。这是因为此
在当前函数中引用。我喜欢做的是有一个变量,它引用了sapConfig
所在的函数实例
例:
下面是您的第一个代码snippit,并实现了我的示例:
"use strict";
var mongojs = require('mongojs'); // MongoDB API wrapper
module.exports = function(regKey) {
var self = this;
this.regKey = regKey;
this.sapConfig = null;
this.get_config = function() {
// Read SAP connection information from our MONGO db
var db = mongojs('mongodb://localhost/MIM', ['Configurations', 'Registrations']);
db.Registrations.find({ key: this.regKey }, function(err1, registration) {
console.log('Reg.find()');
console.log(registration[0]);
db.Configurations.find({ type: registration[0].type }, function(err2, config) {
console.log('Config.find()');
console.log('config=' + config[0].user);
self.sapConfig = 'test';
});
});
}
this.get_result = function() {
return self.sapConfig;
}
}
第二个片段。您正试图从嵌套回调中返回一个值。因为嵌套函数是异步的,所以不能这样做
下面是我如何从嵌套回调返回值:
例2:
下面是使用该示例的代码:
"use strict";
var mongojs = require('mongojs'); // MongoDB API wrapper
module.exports = function(regKey, done) {
// Read SAP connection information from our MONGO db
var db = mongojs('mongodb://localhost/MIM', ['Configurations', 'Registrations']);
db.Registrations.find({ key: regKey }, function(err1, registration) {
console.log('Reg.find()');
console.log(registration[0]);
db.Configurations.find({ type: registration[0].type }, function(err2, config) {
console.log('Config.find()');
console.log('config=' + config[0].user);
done(config[0].user);
});
});
}
//Then wherever you call the above function use this format
// if config is the name of the function export above...
new Config().(regKey, function(result){
console.log(result); //config[0].user value
})
很多很多代码,但我希望你能理解。如果您还有任何问题,请告诉我!干杯。好极了!工作很有魅力,现在你解释了,这很有道理。您知道为什么另一个代码段返回“undefined”,即为什么return语句没有按预期工作吗?很高兴您能理解它。对于return语句问题:不能从嵌套回调中返回值。我将添加另一个示例,说明如何从嵌套回调返回值。就给我几个:)是的!完美的这完全有道理。我不是在想“异步”。非常好的信息。非常感谢你@以实玛利,欢迎你。请将我的答案标记为解决方案:)祝你发展顺利:)第二部分见我答案中的新示例:)干杯。
//Function example
var functionWithNested = function(done) {
//Notice the done param.
// It is going to be a function that takes the finished data once all our nested functions are done.
function() {
//Do things
function() {
//do more things
done("resultHere"); //finished. pass back the result.
}();//end of 2nd nested function
}(); //end of 1st nested function
};
//Calling the function
functionWithNested(function(result) {
//Callback
console.log(result); //resultHere
})
"use strict";
var mongojs = require('mongojs'); // MongoDB API wrapper
module.exports = function(regKey, done) {
// Read SAP connection information from our MONGO db
var db = mongojs('mongodb://localhost/MIM', ['Configurations', 'Registrations']);
db.Registrations.find({ key: regKey }, function(err1, registration) {
console.log('Reg.find()');
console.log(registration[0]);
db.Configurations.find({ type: registration[0].type }, function(err2, config) {
console.log('Config.find()');
console.log('config=' + config[0].user);
done(config[0].user);
});
});
}
//Then wherever you call the above function use this format
// if config is the name of the function export above...
new Config().(regKey, function(result){
console.log(result); //config[0].user value
})