Reputation: 65
so I have a code like this.
var query = new storage.TableQuery().where('PartitionKey eq ?', req.body.RowKey._);
storageClient.queryEntities(fieldtable, query, null, function (error, result, response) {
var modelEntries = {};
var enumList = {};
var enumName;
modelEntries = result.entries;
console.log(modelEntries);
for (var i = 0; i < modelEntries.length; i++){
if (result.entries[i].fielddatatype._ === "Enum"){
enumName = result.entries[i].fieldname._;
query = new storage.TableQuery().where('EnumName eq ?', enumName);
storageClient.queryEntities(enumvalue, query, null, function (error, result, response) {
enumList[enumName] = result.entries;
});
}
}
res.end(JSON.stringify(enumList));
});
My problem is with here.
storageClient.queryEntities(enumvalue, query, null, function (error, result, response) {
enumList[enumName] = result.entries;
});
EnumList is receiving the data when I debug it. But once it is outside of the scope of the function it is empty. I think this is a function scope problem but I'm not so sure.
Upvotes: 0
Views: 43
Reputation: 9950
This happens because queryEntities
is an async function, and res.end()
executes before the for-loop ends. In this case, I'd recommend looking at async module and using async.eachOf(...)
. It allows you to run async calls against a list of 'things' and gives you a place to run a function when all of the async calls are done.
var async = require('async');
// ...
async.eachOf(modelEntries, function(value, key, next) {
if (result.entries[key].fielddatatype._ === "Enum") {
enumName = result.entries[key].fieldname._;
query = new storage.TableQuery().where('EnumName eq ?', enumName);
storageClient.queryEntities(enumvalue, query, null, function (error, result, response) {
enumList[enumName] = result.entries;
next();
});
}
}, function(err) {
res.end(JSON.stringify(enumList));
});
Upvotes: 1