Caching datastore query - node.js

I'm not familiar with node programming, so I'm sure the error here is around syntax.
I'm trying to query datastore and store the returned entity as a const config reference in my function.
For some reason, if I invoke this as part of the body of the function I always get undefined instead of the value. Moving the invocation to the function that is called works, but I need the values to configure the function so it can't be on the first invocation.
Any ideas?
const Datastore = require('#google-cloud/datastore');
const datastore = Datastore();
const query = datastore
.createQuery('ProxyConfig')
.filter('function', '=', 'proxy');
const proxyConfig = loadConfig();
function loadConfig() {
datastore.runQuery(query).then(results => {
console.log("Query executed");
console.log(results);
return results;
});
}
exports.proxy = (req, res) => {
cnf = loadConfig();
console.log(`config is ${cnf}`);
res.status(200).send(cnf);
};

You're not returning anything from the function loadConfig which is why you're seeing undefined. With promises like that you want to return the promise and then get the results. By using .then and returning an item within that function the value will be wrapped inside another promise. So you can write this in two different ways. I'll first use the code above as is:
function loadConfig() {
return datastore.runQuery(query).then(results => {
console.log("Query executed");
console.log(results);
return results;
});
}
exports.proxy = (req, res) => {
const configPromise = loadConfig().then(results => {
const cnf = results;
console.log(`config is ${cnf}`);
res.status(200).send(cnf);
})
.catch(err => {
res.status(500).send(err);
});
};
However, since you are simply forwarding the results of the promise to the calling function this code can be simplified to:
exports.proxy = (req, res) => {
datastore.runQuery(query).then(results => {
const cnf = results;
console.log(`config is ${cnf}`);
res.status(200).send(cnf);
})
.catch(err => {
res.status(500).send(err);
});
};

Related

Controller is not able to pass the response to the index.js

I am new to this. There are a couple of solutions posted for a similar problem but none of them helped.
I have posted below a very simple toy example that will help you to debug.
index.js
const express = require('express')
port=3001
var controller = require('./controller');
app.use('/api', controller);
controller.js
var model = require('./model')
var router = express.Router();
router.get('/bl', function(req, res) {
model.getData( function (err, objects) {
if(err) return res.send(err);
return res.status(200).json(objects);
});
});
module.exports = router;
model.js
const bl = {"hello":"world"}
const getData= (request, response) => {
return(bl);
}
module.exports = {
getData
}
Issue:
invoking : http://localhost:3001/api/bl => no response
, console : no error
Note:
in my model.js, I am querying in the Postgres database, and I can see the results in console.log.
but I don't see any such result when I try to see data using console.log in controller.js. Similar behavior I observed in the above toy example
I can see an error and something worrying me.
The error is in following two lines
// In controller.js
model.getData( function (err, objects) {
// In models.js
const getData= (request, response) => {
You define a function accepting two arguments (request, response) (both of them are objects) and than you call it passing only one argument of type function.
The thing worrying me is the getData function itself.
const bl = {"hello":"world"}
const getData= (request, response) => {
return(bl);
}
Apart from parameters, it is a sync function, but in your question you pointed that in models.js you query in the Postgres database, and you can see the results in console.log; now you give us no details about how you query Postgress, but I suppose you are doing it with something asynchronous (probably pg). The other relevant detail you didn't revealed is if you query Postgress inside or outside the body of getData function. I hope I'm wrong, but since in your example you are returning something define outside the body of the function, I have the doubt.
Let's consider only the good option, you query Postgress inside the body of the function, probably your complete getData function looks like follows.
const getData = (request, response) => {
client.query('SELECT $1::text as message', ['Hello world!'], (err, res) => {
// Here you get your message in console
console.log(err ? err.stack : res.rows[0].message);
client.end();
})
return something;
// I don't know what, but fore sure something wrong
}
To solve both the problems you just need to fix getData (by chance or by cut&paste you are calling it in the right way in controller.js).
Let's start fixing its signature makeing it accept only one argument: the callback function:
const getData = (done) => {
then let's asynchronously "return" the data we got from Postgress through the done callback function.
const getData = (done) => {
client.query('SELECT $1::text as message', ['Hello world!'], (err, res) => {
// We can leave this for debugging purposes
console.log(err ? err.stack : res.rows[0].message);
// Here we are not handling errors from client.end()...
// but we can neglect about this right now
client.end();
// Let's calle the callback function passing it the result
done(err, res);
})
// There's nothing to return
}
It should be enough.
Hope this helps.
You need a third argument, you will mostly see callback or just cb and execute it in the function and pass some data to it. The first parameter false will be the error argument later and bl will be the objects argument that is passed
const getData= (request, response, callback) => {
callback(false, bl);
}
and then pass req and res arguments to it:
model.getData(req, res, function (err, objects) {
if(err) return res.send(err);
return res.status(200).json(objects);
});
Or the modern way to do it with async / await you could return an promise
const getData= (request, response) => {
return new Promise((resolve, reject) => {
if(someErrorAppear){
reject("some error occured");
}
resolve(bl);
})
}
Now you can go with async / await
router.get('/bl', async function(req, res) {
try {
let objects = await model.getData(req, res);
res.status(200).json(objects);
}catch(err){
res.send(err);
}
});
});
There is a simple solution to this.
Actually, the error is in model.js. Try below code:
const bl = {"hello":"world"}
const getData= (callback) => {
callback (bl)
}
module.exports = {
getData
}

Why is async functions return Promise <pending> error in console

I want to use the async function to bring out a particular value from my database to my the function global so I can use it in other parts of my application.
async function dimension() {
const result = await Settings.find({_id : "5d7f77d620cf10054ded50bb"},{dimension:1}, (err, res) => {
if(err) throw new Error(err.message, null);
const holder = res[0].dimension;
return holder;
console.log(holder) /// this print the expected result, that i want to make global
});
return {
result
};
};
console.log(dimension())
but the console.log of the dimension() gives me this
Promise { <pending> }
instead of the same value that
console.log(holder)
gives me nothing.
The problem is you are printing the result of dimension() as soon as you call it, but since this function is async, it returns a promise that is not yet resolved.
You do not need to use async/await here. Settings.find() seems to return a Promise. You can just return directly this Promise and use .then() to do something once that promise is resolved.
Like this :
function dimension () {
return Settings.find({ _id: '5d7f77d620cf10054ded50bb' }, { dimension: 1 }, (err, res) => {
if (err) {
throw new Error(err.message, null);
}
return res[0].dimension;
});
}
dimension().then(result => {
//print the result of dimension()
console.log(result);
//if result is a number and you want to add it to other numbers
var newResult = result + 25 + 45
// the variable "newResult" is now equal to your result + 45 + 25
});
More info on Promises and async/await
You have to await for your result, like this:
const result = await dimension();
console.log(result);
In that case, you don't even to make the original function async, just write it like this:
function dimension() {
return Settings.find({_id : "5d7f77d620cf10054ded50bb"},{dimension:1}, (err, res) => {
if(err) throw new Error(err.message, null);
const holder = res[0].dimension;
return holder;
});
};
async function myGlobalFunc() {
const result = await dimension();
console.log(result);
}
The best way to have this globally available is to just put your function dimension in a file somewhere. Then where you need the value, you just require it and await its value. E.g.
// get-dimension.js
// ...const Settings = require... comes here
module.exports = function getDimension() {
return Settings.find({_id : "5d7f77d620cf10054ded50bb"},{dimension:1}, (err, res) => {
if(err) throw new Error(err.message, null);
const holder = res[0].dimension;
return holder;
});
}
// your other modules, e.g.
// my-service-handler.js
const getDimesion = require('./get-dimension');
async function myServiceHandler() {
const dimension = await getDimension();
// do stuff with dimension.
}
You're using async/await, but you're mixing it with callbacks, this is not desirable as it leads to confusion. It's not clear what you expect to happen in the callback, but return holder; likely doesn't do what you expect it to do, returning from a callback does not work the same way returning from a promise handler works. Your entire implementation should work with promises so that the async/await syntax reads more naturally (as it was intended).
async function dimension() {
// We're already awaiting the result, no need for a callback...
// If an error is thrown from Settings.find it is propagated to the caller,
// no need to catch and rethrow the error...
const res = await Settings.find({_id: "5d7f77d620cf10054ded50bb"}, {dimension: 1});
return {result: res[0].dimension};
}
(async () => {
try {
console.log(await dimension());
} catch (err) {
console.error(err);
}
})();
Use dimension().then() in your code then it will work fine.
async function globalDimension() {
const data = await Users.findOne({ phone: 8109522305 }).exec();
return data.name;
}
globalDimension().then(data => {
console.log(data);
});

Assign value to variable outside mongo query in nodejs

Right now i have this code
router.get('/export', function(req, res, next) {
var postData, eventData, messageData, userData
Posts.list().then(data=> {
var jsonOutput=JSON.stringify(data)
postData=jsonOutput //this doesnt work
})
.catch(erro => res.status(500).send('error'))
Events.list().then(data=> {
var jsonOutput=JSON.stringify(data)
eventData=jsonOutput //this doesnt work
})
.catch(erro => res.status(500).send('error'))
Messages.list().then(data=> {
var jsonOutput=JSON.stringify(data)
messageData=jsonOutput //this doesnt work
})
.catch(erro => res.status(500).send('error'))
Users.list().then(data=> {
var jsonOutput=JSON.stringify(data)
userData=jsonOutput //this doesnt work
})
.catch(erro => res.status(500).send('error'))
//Then when all data from colections is retrieve i want to use the 4 variables that i created in the beggining
});
So basicly im trying to retrieve the data from my mongo database and then assign the results to that 4 variables that i create, but im not getting success.
For what i´ve been seeing i have to use async but im having some trouble doing it.
I don't like too much mrlanlee solution. This is a typical situation where using async / await can really make sense. Anyway, the Hugo's solution (the second one, with async await), even if it just works, will make the four queries in sequence, one after another to. If you want a clean, working and parallel solution, check this:
router.get('/export', async function(req, res, next) {
let data
try {
data = await Promise.all([
Posts.list(),
Events.list(),
Messages.list(),
Users.list()
]);
// at this point, data is an array. data[0] = Posts.list result, data[1] = Events.list result etc..
res.status(200).json(data)
} catch (e) {
res.status(500).send('error');
}
});
The other answer from Sashi is on the right track but you will probably run into errors. Since your catch statement on each promise returns 500, if multiple errors are caught during the query, Express will not send an error or 500 each time, instead it will throw an error trying to.
See below.
router.get('/export', function(req, res, next) {
var postData, eventData, messageData, userData
try {
postData = Posts.list().then(data=> {
return JSON.stringify(data);
});
eventData = Events.list().then(data=> {
return JSON.stringify(data)
});
messageData = Messages.list().then(data=> {
return JSON.stringify(data);
})
userData = Users.list().then(data=> {
return JSON.stringify(data)
});
} catch (err) {
// this should catch your errors on all 4 promises above
return res.status(500).send('error')
}
// this part is optional, i wasn't sure if you were planning
// on returning all the data back in an object
const response = {
postData,
eventData,
messageData,
userData,
};
return res.status(200).send({ response })
});
For explanation of why you weren't able to mutate the variables, see Sashi's answer as he explains it.
The variables defined outside the async code is out of scope of the async functions. Hence you cannot store the returned value from the async functions in those variables.
This should work.
router.get('/export', function(req, res, next) {
var postData, eventData, messageData, userData
postData = Posts.list().then(data=> {
var jsonOutput=JSON.stringify(data);
return jsonOutput;
}).catch(erro => res.status(500).send('error'));
eventData = Events.list().then(data=> {
var jsonOutput=JSON.stringify(data);
return jsonOutput;
}).catch(erro => res.status(500).send('error'));
messageData = Messages.list().then(data=> {
var jsonOutput=JSON.stringify(data);
return jsonOutput;
}).catch(erro => res.status(500).send('error'));
userData = Users.list().then(data=> {
var jsonOutput=JSON.stringify(data);
return jsonOutput;
}).catch(erro => res.status(500).send('error'));
});
Using Async/Await is a much neater solution.
router.get('/export', async function(req, res, next) {
var postData, eventData, messageData, userData;
try{
postData = await Posts.list();
eventData = await Events.list();
messageData = await Messages.list()
userData = await Users.list();
catch (e){
res.status(500).send('error');
}
});

Nest multiple async await

I have the following Express endpoint:
const all = require('promise-all');
router.post('/verify', upload.single('photo'), async (req, res) => {
...
await all({'p1': p1, 'p2': p2}).then((response) => {
...
console.log("Response:",
ruleCtrl.manageRule(detection, res);
});
});
ruleCtrl.manageRuleis as follows:
export async function manageRule(identifierDetected, res) {
let rule = db.getRule(identifierDetected);
await all([rule]).then((ruleExtracted) => {
...
res.json(ruleExtracted);
}).catch((err) => {
res.status(418).send("DOCUMENT_NOT_RECOGNIZED");
});
}
and db.getRule:
export async function getRule(idRule) {
return new Promise((resolve, reject) => {
Rule.findOne({ruleID: idRule}, (err, rule) => {
if (err) {
reject("MongoDB Rule error: " + err);
} else {
resolve(rule);
}
});
})
}
My response is into manageRule and this function depends of the values extracted into the await all. So, right now, Express is returning a response before get the information from mongoose database (db).
Which is the way to handle this issue?
Thanks everyone!
I would refactor your code a bit to make it easier to read, and also return the result from ruleCtrl.manageRule(detection, res);.
The request might simply be timing out since your original code is missing a return there or an await (to make sure it finishes executing)
Express endpoint:
const all = require('promise-all');
router.post('/verify', upload.single('photo'), async (req, res) => {
...
// Catch any exceptions from the promises. This is the same as using .catch
try {
// Lets assign the returned responses to variable
let [p1Result, p2Result] = await all({'p1': p1, 'p2': p2});
...
console.log("Responses:", p1Result, p2Result);
// return the response from manageRule method
return ruleCtrl.manageRule(detection, res);
} catch(err) {
// Handle err here
}
});
One of the great benefits with async await is moving away from chained promises, so simply return the result from the await to a variable instead of using .then()
ruleCtrl.manageRule
export async function manageRule(identifierDetected, res) {
// Use try catch here to catch error from db.getRule. Assign to variable and return
// res.json
try {
let ruleExtracted = await db.getRule(identifierDetected);
...
return res.json(ruleExtracted);
} catch(err) {
return res.status(418).send("DOCUMENT_NOT_RECOGNIZED");
}
}
You dont have to return res.json or res.status here, I just like to keep track of when I want to end function execution.
You could refactor the ruleCtrl.manageRule method even further by not sending in res as a parameter but by returning the result from db.getRule instead. Let router.post('/verify) handle req and res, so to make it even easier to read.

How to return a value from a function

I have an ArangoDB function that queries all the documents in the instanceCollection, inside databasehelper.js:
listInstances = function listInstances() {
var instances = db.query(aqlQuery`
FOR doc in instanceCollection
RETURN doc
`).then(
cursor => cursor.all()
).then(
result => {
return result;
}
)
return instances;
}
Also, I want to use this function to reflect the JSON query to an Express API:
app.get('/', function(req, res) {
var allInstances = databasehelper.listInstances();
res.send(allInstances);
});
The api result was nothing but an empty JSON {}.
How can I reflect the result of my ArangoDB query to my Express API call?
Understand how promises work. The following is an idea on how it should be designed.
databasehelper.js => Return a promise
listInstances = function listInstances() {
return db.query(`some query`)
.then(cursor => cursor.all())
}
in the route, => fetch the data from the promise
app.get('/', function(req, res) {
databasehelper.listInstances()
.then(result => {
res.send(result);
})
});

Resources