我仍然是NodeJS的n00b,所以这个问题可能有点基本。
我正在使用MongoJS来阅读两个逻辑相关的集合。第一个 find()返回一个值,我传递给第二个 find()以获取我需要的信息。
我尝试了几种策略,最后一种(片段#1)是我导出的类。
在此之前我只有一个执行返回的功能,返回所需的值,即“config [0]”。
在这段代码中,我所做的就是将“sapConfig”属性设置为单词“test”,但是当我执行此代码时,在调用“get_config()”方法后,“sapConfig”的值始终为“null”并且 - 最奇怪的是 - 对“this.sapConfig ='test'”的引用会生成错误,即“无法设置未定义的属性'sapConfig'。”
当我将代码作为带有 return 语句(代码段#2)的简单函数时,没有生成错误但返回的值总是“未定义”,尽管console.log()语句显示返回的变量的值具有所需的值。是什么给了什么?
代码段#1:返回对象
"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;
}
}
同样,当我调用“get_config()”时,代码段#1中的代码在执行“this.sapConfig ='test'”行时会导致错误。
然而,在这个错误之后我可以执行“obj.get_result()”并获得它被初始化的值,即null。换句话说,在“get_config()”方法中,相同的代码不会产生错误,表示“this”未定义为。
代码片段#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;
});
});
}
当我收到返回值并检查它时,它是“未定义的”。例如,在Node 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
您可以看到查询成功但“k”的值为“未定义”。这是怎么回事?
我不关心我使用哪种方法我只需要其中一种工作。
提前致谢!
答案 0 :(得分:0)
this.sapConfig
无法访问。那是因为this
指的是当前函数。我喜欢todo,有一个变量引用你知道sapConfig
所在的函数实例。
例如:
function Foo() {
var self = this;
this.test = "I am test";
var bar = function(){
return function(){
console.log(this.test); //outputs undefined (because this refers to the current function scope)
console.log(self.test); //outputs "I am test";
}
}
}
以下是您实施我的示例的第一个代码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:
//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
})
很多代码,但我希望你能够遵循它。如果您还有其他问题,请与我们联系!欢呼声。