I'm trying to set a global variable for the function in order to use it in all functions inside a scope. Functions are called one after another using async.series
:
exports.update = function(req, res, next){
var result = {}; // setting global var for the scope
var function1 = function(callback) {
MyModel.findOne(conditions)
.lean()
.exec(function(err, docs) {
if (err) {
return callback(err, null);
}
result.docs = docs; // assigning function result to global var
return callback(null, 'done');
});
};
var function2 = function(callback) {
var fieldsToSet = {
// ...
somefield: result.docs.someproperty // error here result.docs = null
};
Mymodel.create(fieldsToSet, function(err, record) {
// ...
});
};
};
require('async').series([function1, function2]);
};
Returns error:
TypeError: Cannot read property 'someproperty' of null
How in this case I get the property? Thank you.
Old answer
Declare result
outside of exports.update
. Must work in this case.
EDIT : Probably make result
equal to an empty object inside the function as well, if that's what you want.
New answer as of December 17th
@ASem : if you're still up for it, maybe define result.docs = {};
right under result
?
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.