This is more a general JS question and not specific to edge.js. I wanted to play with SQL Server access via edge.js. Based on the following snippet from its GitHub site
var edge = require('edge');
var getTop10Products = edge.func('sql', function () {/*
select top 10 * from Products
*/});
getTop10Products(null, function (error, result) {
if (error) throw error;
console.log(result);
console.log(result[0].ProductName);
console.log(result[1].ReorderLevel);
});
I wrote the following to access my own database:
var edge = require('edge');
var _ = require('lodash');
var thingsAccess = function () {/*
select * from Things where Status = 'A' and Type=@thingType
*/};
var getThingsList = edge.func('sql', thingsAccess);
var logResult = function (error, result) {
if (error) throw error;
_.each(result, function (thing) {
console.log(thing.Id, thing.Category, thing.Name);
});
};
var thingsOfType = function (type) {
return function () {
getThingsList({ thingType: type }, logResult);
};
};
var xThings = thingsOfType('X');
var yThings = thingsOfType('Y');
xThings();
yThings();
My question is, how can I return results from logResult rather than just using the data inside that function? Instead of the _.each(result, ...) construct that now logs to the console, I'd rather have something like return _.map(result, ...) that returns an array of Thing objects. But because the function is a callback for getThingsList, I have nowhere to put the results.
Given how I have structured the code, it seems like my only option is to declare an array in the outer scope and push to it from inside logResult. I'm sure I'm doing it wrong, but I can't see how to restructure. Any suggestions?