Returning array of undefined?

24 views
Skip to first unread message

Kevin Burton

unread,
Jan 13, 2016, 2:37:05 PM1/13/16
to Q Continuum (JavaScript)
I am missing something in my understanding of promises so please bear with me. 

Currently I have code like:
var sql = require ('mssql');

var ConvertToJSON = function() {
var config = {
server: 'db3dev',
database: 'EForms',
driver: 'msnodesqlv8',
options: {
trustedConnection: true
}
};
return sql.connect(config).then(function(result) {
var formRequest = new sql.Request();
var formQuery = 'select *' +
' from [EForms].[OnlineForm].[FormDefinition]';
return formRequest.query(formQuery).then(function(formRows) {
return Promise.all(formRows.map(function(formRow) {
var pageRequest = new sql.Request();
var pageQuery = 'select *' +
' from [EForms].[OnlineForm].[PageDefinition] pd' +
' where pd.formDefinitionID = ' + formRow.formDefinitionID;
pageRequest.query(pageQuery).then(function(pageRows) {
return Promise.all(pageRows.map(function(pageRow) {
var fieldRequest = new sql.Request();
var fieldQuery = 'select *' +
' from [EForms].[OnlineForm].[FieldDefinition] fd' +
' where fd.pageDefinitionID = ' + pageRow.pageDefinitionID;
fieldRequest.query(fieldQuery).then(function(fieldRows) {
return Promise.all(fieldRows.map(function(fieldRow) {
return fieldRow.fieldKey;
}));
});
}));
});
}));
});
})
}
ConvertToJSON().then(function(result) {
console.log('Success!!');
}).catch(function(err) {
console.log(err);
});

The idea is to return a promise on connection that will only get resolved when all forms, pages, and fields are resolved. But the function 'ConvertToJSON' returns an array of 17 undefined values. Since the inner most query is 'resolving' to a string (fieldKey) I was expecting the result to be an array of strings.

Ideas?

Thank you.

Kris Kowal

unread,
Jan 13, 2016, 5:12:25 PM1/13/16
to Q Continuum
Missing return for the line starting with pageRequest.query. Hope that helps.

--
You received this message because you are subscribed to the Google Groups "Q Continuum (JavaScript)" group.
To unsubscribe from this group and stop receiving emails from it, send an email to q-continuum...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Kevin Burton

unread,
Jan 14, 2016, 11:47:26 AM1/14/16
to Q Continuum (JavaScript), kr...@cixar.com
Thank you. DUH! This was clearly an issue that I just needed another set of eyes.
Reply all
Reply to author
Forward
0 new messages