javascript - Having issues responding to an HTTP request with the proper array. Possible way to send results from within function with expressjs? -


problem summary: i'm working on program has sql database on backend , i'm using npm sqlite3 module talk it. when make http request server told in post should res.send function inside after function ran, return whatever came function. easy enough right? ran issue asynchronous function .all (api doc here). happens call database , returns inside object inside database.all, database.all asynchronous function void , can't return anything. i've got callback sends object inside callback, don't know there, or if need callback.

so question is: can send http response somewhere inside .all function or callback, or function have return array called? code below show mean.

chain of events (github repo refrence):

i've set angular controller send following http request when page loads:

$http.get('/loadwaitinglist') .then(function(response) {     // alert("http request set, getting data");     console.log(response.data); }); 

and when server (server.js) receives it, this:

app.get('/loadwaitinglist', function (req, res) {   res.send(databasefunction.loadwaitinglist()); }) 

which runs loadwaitinglist() function in file named test.js:

function  loadwaitinglist() {     var callbackfunction = function(err, response){         var returnrow = response;         return returnrow;     }     updatedb.manager(callbackfunction); } 

which calls updatedb.manager, aka guy:

manager : function(callback){     var fs = require("fs");     var file = "./source/server/data/daycaredb.db";     var exists = fs.existssync(file);     if (!exists) {         throw new error("file not found");     }     var sqlite3 = require("sqlite3").verbose();     var db = new sqlite3.database(file);     db.all("select * waitinglist", function(err, row) {         if (err){             callback(err);             return;         }         // console.log(row[0].childname);                            callback(null, row);         return;     }); }, 

so can see, after db.all runs, values i'm after after in row. row passed db.all parameter , returned array, i've tried declaring outside db.all , passing in , printing out results, due db.all being asynchronous comes empty because function hasn't run yet.

when run callback , check object (using vscode debugger) after callback has been called, returnrow has proper data in database.

how send data gets put row , returnrow response? possible initial function call when http request different way , res.send callback or something?

you need @ callbacks.

app.get('/loadwaitinglist', function (req, res) {   databasefunction.loadwaitinglist(function(err,data){     if(err) {       // handle error here     }     // send data     res.send(data);   } }); 

now loadwaitinglist() needs take standard error-first callback parameter. callback passed through updatedb.manager() executed.

function loadwaitinglist(callback) {   updatedb.manager(callback); } 

Comments

Popular posts from this blog

php - How to display all orders for a single product showing the most recent first? Woocommerce -

asp.net - How to correctly use QUERY_STRING in ISAPI rewrite? -

angularjs - How restrict admin panel using in backend laravel and admin panel on angular? -