Node js, how to run a code sync after a loop - javascript

here is my code in which I try to update some documents of my database (MongoDB)
using a for loop. however I want to run the next code after the loop is completed,
as for example, I want to use some variables calculated inside the loop after it finished.
How can I do such using callback, promise, etc?
numPktUpdated = 0;
for (key in InvoicesPUT) {
Invoice.findOneAndUpdate({ InvoiceNumber: InvoicesPUT[key].InvoiceNumber }, InvoicesPUT[key]).exec()
.then((doc) => {
console.log("Update Succeeded")
numPktUpdated = numPktUpdated + 1;
})
.catch((err) => {
return resp.send(JSON.stringify({
"status": "error",
"message": "DB Error while Updating: Wrong Packet"
}));
console.log(err);
})
}
resp.send(numPktUpdated);
Here numPktUpdated = 0 is sent to client, although its real value after the loop is something else.
Thanks.

You should be able to do this with Promise.all():
Promise.all(InvoicesPUT.map(InvoicePUT => {
return Invoice.findOneAndUpdate({
InvoiceNumber: InvoicePUT.InvoiceNumber
}, InvoicePUT).exec()
.then(doc => {
console.log("Update Succeeded");
numPktUpdated += 1;
})
}))
.catch(err => {
return resp.send(JSON.stringify({
"status": "error",
"message": "DB Error while Updating: Wrong Packet"
}));
console.log(err);
})
.then(() => {
// What you want to do after the loop ...
})

Try to put code to the function, then run it with async
async function forLoopFunction(args){//here is a for loop
}
let ret = await forLoopFunction(args);

If you are using an older version of Node v7.6, which brought support for the async / await pattern, you could use a simple Promise:
function updateDocs() {
return new Promise(function(resolve, reject) {
numPktUpdated = 0;
for (key in InvoicesPUT) {
Invoice.findOneAndUpdate({ InvoiceNumber: InvoicesPUT[key].InvoiceNumber }, InvoicesPUT[key]).exec()
.then((doc) => {
console.log("Update Succeeded")
numPktUpdated = numPktUpdated + 1;
resolve(numPktUpdated);
})
.catch((err) => {
console.log(err);
reject(err);
return resp.send(JSON.stringify({
"status": "error",
"message": "DB Error while Updating: Wrong Packet"
}));
})
}
});
}
updateDocs().then(function(numPktUpdated) {
resp.send(numPktUpdated);
})
.catch(function(err) {
// handle error
});

Thanks, It worked with just a minor modification:
Promise.all(InvoicesPUT.map(invoice => {
return Invoice.findOneAndUpdate({InvoiceNumber: invoice.InvoiceNumber}, invoice).exec()
.then((doc) => {
console.log("Update Succeeded")
numPktUpdated = numPktUpdated + 1;
})
}))
.catch((err) => {
return resp.send(JSON.stringify({
"status": "error",
"message": "DB Error while Updating: Wrong Packet"
}));
console.log(err);
})
.then((resolve) => {
return resp.send(JSON.stringify({
"status": "succed",
"LastSavedGUID": 654323,
"SyncDate": 1,
"pagenumber": numPktUpdated
}));
})

Related

Node/Express return error to main function?

I'm in a situation where I have a POST route that calls a function multiple times. I want the request to return an error if the called function returns an error, but I am not sure how to achieve this. See this image:
This is my code:
function POSTcord(lat, lng) {
axios
.post(process.env.SOS_POST_URL + process.env.SOS_POST_CODE, {
batteryLevel: 100,
longitude: lng,
latitude: lat
})
.then(res => {
console.log(`statusCode: ${res.status}`)
})
.catch(error => {
console.error(error.message);
})
}
router.post('/test', async (req, res) => {
let passedCords = req.body;
try {
for (const cord of passedCords) {
POSTcord(cord.lat, cord.lng);
}
res.status(200).json({status:"success", message: "hello!"});
} catch (err) {
console.error(err.message);
res.status(500).send("Server error");
}
});
I want the route /test to return an error if the function POSTcord catches an error somewhere in the loop. Any ideas on this? I'm thinking I could pass res to POSTcord function, but that didn't work. Thankful for any input :)
You need to return the Promise and make sure the error is thrown/rejected:
Either do this:
function POSTcord(lat, lng) {
return axios // <--------------- THIS IS VERY IMPORTANT
.post(process.env.SOS_POST_URL + process.env.SOS_POST_CODE, {
batteryLevel: 100,
longitude: lng,
latitude: lat
})
.then(res => {
console.log(`statusCode: ${res.status}`)
})
.catch(error => {
console.error(error.message);
throw error; // <----------- ALSO DO THIS
})
}
Or do this:
function POSTcord(lat, lng) {
return axios // <--------------- THIS IS VERY IMPORTANT
.post(process.env.SOS_POST_URL + process.env.SOS_POST_CODE, {
batteryLevel: 100,
longitude: lng,
latitude: lat
})
.then(res => {
console.log(`statusCode: ${res.status}`)
})
// DON'T CATCH THE ERROR!!
}
Then all you need to do is await to get the error:
router.post('/test', async (req, res) => {
let passedCords = req.body;
try {
for (const cord of passedCords) {
await POSTcord(cord.lat, cord.lng); // DO THIS FOR CATCH TO WORK
}
res.status(200).json({status:"success", message: "hello!"});
} catch (err) {
console.error(err.message);
res.status(500).send("Server error");
}
});
If you want to call POSTcord() in parallel you can await using Promise.all():
router.post('/test', async (req, res) => {
let passedCords = req.body;
try {
let promises = [];
for (const cord of passedCords) {
let p = POSTcord(cord.lat, cord.lng);
promises.push(p);
}
await Promise.all(promises); // DO THIS FOR CATCH TO WORK
res.status(200).json({status:"success", message: "hello!"});
} catch (err) {
console.error(err.message);
res.status(500).send("Server error");
}
});

Should i use promise or callback in the following code?

I have some routes
routes.js
var express = require("express");
var router = express.Router();
const { controllerMethod1, controllerMethod2 } = require("./controller");
router.get("/route1", controllerMethod1);
router.get("/route2", controllerMethod2);
module.exports = router;
if i use promise variable as global,
its used by all method in controller.js.
should i use global or local variable for promise ?
controller.js
const {
serviceMethod1,
serviceMethod2,
serviceMethod1ByDate,
} = require("./services");
let promise; //global promise variable
const controllerMethod1 = (req, res) => {
//let promise; local promise variable
//This is for Callback
if (req.query.date) {
serviceMethod1ByDate(req.query.date, (err, result) => {
if (err) {
res.status(500).json({
status: "error",
message: "error using callback",
});
}
if (result) {
res.status(200).json({
status: "success",
message: "success using callback",
});
}
});
} else {
serviceMethod1((err, result) => {
if (err) {
res.status(500).json({
status: "error",
message: "error using callback",
});
}
if (result) {
res.status(200).json({
status: "success",
message: "success using callback",
});
}
});
}
// This is for Promise
promise = req.query.date
? serviceMethod1ByDate(req.query.date)
: serviceMethod1();
Should i use way 1 or way 2 ?
if multiple users request one or more routes at the same time,can handleResponse method work correctly?
Way 1 for promise
promise
.then((results) => {
return res.json({
status: "success with promise variable",
data: results,
});
})
.catch((error) => {
return res.status(500).json({
status: "error with promise variable",
message: "there is no person details",
});
});
Way 2 for Promise
handleResponse(promise, res);
//this method is working for all routes when i use promise
const handleResponse = (results, response) => {
results
.then((result) => {
return response.json({
status: "success with promise variable in handleResponse",
data: result,
});
})
.catch((error) => {
return response.status(500).json({
status: "error with promise variable handleResponse",
message: "Internal Server Error",
});
});
};
controller.js
const controllerMethod2 = (req, res) => {
//------------------ Using Callback Method -------------
serviceMethod2((err, result) => {
if (err) {
res.status(500).json({
status: "error",
message: "error using callback",
});
}
if (result) {
res.status(200).json({
status: "success",
message: "success using callback",
});
}
});
//------------------ Using Promise Method -------------
//local variable
let promise;
promise = serviceMethod2();
//Way 1 for Promise
promise
.then((result) => {
//...
})
.catch((err) => {
//...
});
//Way 2 for Promise
handleResponse(promise, res);
};
module.exports = { controllerMethod1, controllerMethod2 };
service.js
const pool = require("../../../config/database");
//-----------------------Using Callback Mehthod----------------
const serviceMethod1 = async (CallBack) => {
let query = "select * from databse";
await pool.query(query, [], (error, results, fields) => {
if (error) {
return CallBack(error);
}
return CallBack(null, results);
});
};
const serviceMethod1ByDate = async (date) => {
let query = "select * from databse where date ?";
return await new Promise((resolve, reject) => {
pool.query(query, [date], (error, results, fields) => {
if (error) {
return CallBack(error);
}
return CallBack(null, results);
});
});
};
const serviceMethod2 = async (Callback) => {
let query = "select * from database";
await pool.query(query, [], (error, results, fields) => {
if (error) {
return CallBack(error);
}
return CallBack(null, results);
});
};
//-----------------------Using Promise Method----------------
const serviceMethod1 = async () => {
let query = "select * from databse";
return await new Promise((resolve, reject) => {
pool.query(query, [], (error, results, fields) => {
if (results) {
resolve(results);
} else {
reject(error);
}
});
});
};
const serviceMethod1ByDate = async (date) => {
let query = "select * from databse where date ?";
return await new Promise((resolve, reject) => {
pool.query(query, [date], (error, results, fields) => {
if (results) {
resolve(results);
} else {
reject(error);
}
});
});
};
const serviceMethod2 = async () => {
let query = "select * from database";
return await new Promise((resolve, reject) => {
pool.query(query, [], (error, results, fields) => {
if (results) {
resolve(results);
} else {
reject(error);
}
});
});
};
module.exports = {
serviceMethod1,
serviceMethod1ByDate,
serviceMethod2,
};
if i use promise variable as global, its used by all method in controller.js. should i use global or local variable for promise ?
You should use local variable for this type of operation as global variable are generally used to define constants or methods. They cannot be used as temporary values because it's value can be changed anytime and that'll result in conflict with other functionalities and so must be avoided.
Should i use way 1 or way 2 ? if multiple users request one or more routes at the same time,can handleResponse method work correctly?
Way 2 is more efficient that Way 1 because if you use way 1 then you will have to do it for every method in the controller. Way 2 is like a common method where you can format your response and most of the developers use it.
It doesn't make any difference whether you use callbacks or promises but just a clean way to do things.
Instead of using this:
const serviceMethod1 = async () => {
let query = "select * from databse";
return await new Promise((resolve, reject) => {
pool.query(query, [], (error, results, fields) => {
if (results) {
resolve(results);
} else {
reject(error);
}
});
});
};
Use this:
// Remove the async await from here and handle the response/error where the below method is called by putting it in try catch block.
const serviceMethod1 = () => {
let query = "select * from databse";
return new Promise((resolve, reject) => {
pool.query(query, [], (error, results, fields) => {
if (results) {
resolve(results);
} else {
reject(error);
}
});
});
};
// otherFile.js
someMethod = async () => {
try {
const result = await serviceMethod1();
// handle the response
} catch {
// handle the error
}
}

How to handle errors from within a callback in Nodejs

I have this code that invokes a function and has a callback with error and data parameters:
app.get('/lights', (req,res) => {
hue.getLights(function(err, data){
if(err) res.status(401).send("An error occured: ", err.message);
res.send(data);
});
})
The function that it invokes is:
let getLights = function(callback){
fetch(`http://${gateway}/api/${username}/lights`, {
method: 'GET'
}).then((res) => {
if(res.ok){
return res.json();
}else{
throw new Error(res.message);
}
}).then((json) => {
lightsArray = []
for (var i in json){
lightsArray.push(`ID: ${i} Name: ${json[i]['name']}`);
}
return callback(lightsArray);
});
}
When I make an error occur, the error isn't caught, nor is any error displayed, the app crashes with the message: UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch().
Now I know I'm missing a lot, this is my first time using callbacks, let alone handling errors.
Could someone help me with making the error callback work, also show me some flaws in what I'm doing, as I know this won't catch every error that may happen, only errors caused by using the fetch function.
Thanks!
This is my other function (similar but uses a catch aswell, which I think I have done incorrectly too):
let getLightDetails = function (ID, callback) {
fetch(`http://${gateway}/api/${username}/lights/${ID}`, {
method: 'GET'
}).then((res) => {
if(res.ok){
return res.json();
}else{
throw new Error(res.message);
}
}).then((json) => {
return callback(json);
})
.catch((err) => {
console.log(err);
return callback(err.message);
});
}
Mixing callbacks and promises can make your code a bit messy. I would stick to promises:
app.get('/lights', (req, res) => {
return hue.getLights()
.then(data => {
res.send(data);
})
.catch(err => {
res.status(401).send("An error occured: ", err.message);
});
})
and hue.js
const fetch = require('node-fetch');
const gateway = "192.168.0.12";
const username = "username-A";
function fetchAPI(url, ...rest) {
return fetch(`http://${gateway}/api/${username}${url}`, ...rest);
}
function getLights() {
return fetchAPI(`/lights`)
.then(res => res.json())
.then(json => json.map((light, i) => `ID: ${i} Name: ${light.name}`));
}
function getLightDetails(id) {
return fetchAPI(`/lights/${id}`)
.then(res => res.json());
}
function getLightState(id) {
return fetchAPI(`/lights/${id}`)
.then(res => res.json())
.then(light => `Name: ${light.name} On: ${light.state.on}`);
}
function setLightState(id, state) {
return fetchAPI(`/lights/${id}/state`, {
method: 'PUT',
body: JSON.stringify({"on": state })
}).then(res => res.json());
}
module.exports = { getLights, getLightDetails, getLightState, setLightState };

Best Practice for chaining promises in Nodejs/Express

I know that best way to chain promises in Nodejs/Express is like:
doSomeThing()
.then()
.then()
.catch();
But recently had to use the async and q module to iterate over a list/array and run an async function. I wanted to know that is there better way of doing/writing this -
var deferred = Q.defer();
var deferred2 = Q.defer();
models.Local.findOne({
where: {
id: parseInt(req.body.localid)
}
})
.then(function(resultLocal){
if(!resultLocal){
return res.status(404).json(
{
"status" : "error",
'error': "Local Not Found"
});
}
return models.Documents.create(req.body.document);
})
.then(function(docCreated){
var attributes = req.body.document.Attributes;
async.each(attributes, function(item, callback) {
models.Doc_Tags.create({
value: item.value,
attribute_id: item.id,
document_id: docCreated.id
})
.then(function(attributeCreated){
var upObj = {};
upObj[item.col_name] = item.value;
models[item.table_name].update(upObj,{
where:{
id: req.body.document.local_id
}
})
.then(function(primaryUpdated){
deferred2.resolve();
})
.catch(function(error){
return res.status(400).json({status: 'error', error:error.message});
});
deferred2.promise
.then(function(){
callback();
})
.catch(function(error){
return res.status(400).json({status: "error", error: error.message});
});
})
.catch(function(error){
return res.status(400).json({status: 'error', error:error.message});
});
}, function(err,r){
if( err ) {
return res.status(400).json({status: 'error', error:err.message});
} else {
console.log('All attributes Associated');
deferred.resolve(docCreated);
}
});
deferred.promise.then(function(result, attributes){
var obj = req.body.Local;
models.Local.update(obj, {
where: {
id: result.local_id
}
})
.then(function(resultUpdate){
return res.status(201).json({status: "success", document: result});
})
.catch(function(error){
return res.status(400).json({status: "error", error: error.message});
});
})
.catch(function(error){
return res.status(400).json({status: "error", error: error.message});
});
})
.catch(function(error){
return res.status(400).json({status: "error", error: error.message});
});
Please correct me if I am doing something wrong. Functionality wise the code is running properly but I think I can refactor it somehow to look and read better.
Thanks.
your code can be cleaner and shorter.
basic ideas are
turn callback to promise, e.g., promisify() of bluebird.js can do that
async.each part can be refactor to Promise.all to call promise parallelly
re-arrange .then chain
javascript es6 is cleaner than older version
sample refactored version
const Promise = require('bluebird')
// CustomError should be separated to another node module
class CustomError {
constructor(message, code) {
this.code = code
this.message = message
}
}
let docCreated = undefined
function getPromiseParams(item) {
return Promise.try(() => {
return models.Doc_Tags.create({
value: item.value,
attribute_id: item.id,
document_id: docCreated.id
})
}).then(attributeCreated => {
const upObj = {};
upObj[item.col_name] = item.value;
return models[item.table_name].update(upObj, { where:{ id: req.body.document.local_id } })
}).then(primaryUpdated => {
return docCreated
}).catch(error => {
throw new CustomError(error.message, 400)
})
}
Promise.try(() => {
return models.Local.findOne({ where: { id: parseInt(req.body.localid) } })
}).then(resultLocal => {
if(!resultLocal) throw new CustomError('Local Not Found', 404)
return models.Documents.create(req.body.document)
}).then(_docCreated => {
docCreated = _docCreated // assign value to docCreated
const attributes = req.body.document.Attributes
const promiseParams = attributes.map(item => getPromiseParams(item))
return Promise.all(promiseParams)
}).then(() => {
const obj = req.body.Local
return models.Local.update(obj, { where: { id: result.local_id }})
}).then(() => {
return res.status(201).json({status: "success", document: docCreated})
}).catch(error => {
return res.status(error.code || 400).json({status: "error", error: error.message});
})

ES7 timeout for async/await fetch

I want to set timeout in my fetch method, and I follow this github issue.
from #mislav answer, we could custom timeout method like below.
// Rough implementation. Untested.
function timeout(ms, promise) {
return new Promise(function(resolve, reject) {
setTimeout(function() {
reject(new Error("timeout"))
}, ms)
promise.then(resolve, reject)
})
}
timeout(1000, fetch('/hello')).then(function(response) {
// process response
}).catch(function(error) {
// might be a timeout error
})
and improved by #nodkz
function timeoutPromise(ms, promise) {
return new Promise((resolve, reject) => {
const timeoutId = setTimeout(() => {
reject(new Error("promise timeout"))
}, ms);
promise.then(
(res) => {
clearTimeout(timeoutId);
resolve(res);
},
(err) => {
clearTimeout(timeoutId);
reject(err);
}
);
})
}
but I want to call that method in ES7 async/await syntax, I try below way but failed.
async request() {
try {
let res = await timeout(1000, fetch('/hello'));
} catch(error) {
// might be a timeout error
}
}
and how could I use it in ES7 async/await syntax?
thanks for your time.
update
thanks for #Bergi reply, and I display my code in http request step.
ES7 fetch
'use strict';
import { configApi, } from 'tyrantdb-config';
import { timeoutPromise, } from 'xd-utils-kit';
const keyResponse = configApi.keyResponse;
const KEY_DATA = keyResponse.data;
module.exports = {
async fetchLogin(url, params, dataRely) {
let {
self, processor, route,
storage, isLocalStoraged,
email, password, _warning, } = dataRely;
self.setState({ isWait: true, });
try {
let res = await timeoutPromise(10, fetch(url, params));
if (res.status >= 200 && res.status < 300) {
let resJson = await res.json();
let resData = resJson[KEY_DATA];
let cache = {
...resData,
password,
};
if (isLocalStoraged !== true) {
processor(cache, storage);
}
global.g_user = cache;
self.setState({ isWait: false, });
// clean user info which already bind to self[this]
self.email = self.password = null;
self.isLocalStoraged = false;
route();
} else {
console.log(`[login] response code: ${res.status}`);
self.setState({ isWait: false, });
_warning();
}
} catch(error) {
console.error(error);
}
},
saveLoginState: (cache, storage) => {
storage.save({
key: 'loginState',
rawData: cache,
});
},
openURL: (url, Linking) => {
Linking.canOpenURL(url).then(supported => {
if (!supported) {
console.log(`can\'t handle url: ${url}`);
} else {
return Linking.openURL(url);
}
}).catch((err) => {
console.log(`error occurred: ${err}`);
})
},
};
ES6 fetch
'use strict';
import { configApi, } from 'tyrantdb-config';
import { timeoutPromise, } from 'xd-utils-kit';
const keyResponse = configApi.keyResponse;
const KEY_DATA = keyResponse.data;
module.exports = {
fetchLogin(url, params, dataRely) {
let {
self, processor, route,
storage, isLocalStoraged,
email, password, _warning, } = dataRely;
self.setState({ isWait: true, });
timeoutPromise(1000, fetch(url, params))
.then((res) => {
if (res.status >= 200 && res.status < 300) {
return res.json();
} else {
console.log(`[login] response code: ${res.status}`);
self.setState({ isWait: false, });
_warning();
}
})
.then((resJson) => {
let resData = resJson[KEY_DATA];
let cache = {
...resData,
password,
};
if (isLocalStoraged !== true) {
processor(cache, storage);
}
global.g_user = cache;
self.setState({ isWait: false, });
// clean user info which already bind to self[this]
self.email = self.password = null;
self.isLocalStoraged = false;
route();
})
.catch(error => console.error(error))
.done();
},
saveLoginState: (cache, storage) => {
storage.save({
key: 'loginState',
rawData: cache,
});
},
openURL: (url, Linking) => {
Linking.canOpenURL(url).then(supported => {
if (!supported) {
console.log(`can\'t handle url: ${url}`);
} else {
return Linking.openURL(url);
}
}).catch((err) => {
console.log(`error occurred: ${err}`);
})
},
};
above ES6 fetch will catch output below, I think #Bergi is right, It's my code fault, not timeoutPromise error.
2016-06-08 22:50:59.789 [info][tid:com.facebook.React.JavaScript] [login] response code: 400
2016-06-08 22:50:59.808 [error][tid:com.facebook.React.JavaScript] { [TypeError: undefined is not an object (evaluating 'KEY_DATA')]
line: 103795,
column: 29,
sourceURL: 'http://172.26.129.189:8081/index.ios.bundle?platform=ios&dev=true' }
2016-06-08 22:50:59.810 [error][tid:com.facebook.React.JavaScript] One of the sources for assign has an enumerable key on the prototype chain. This is an edge case that we do not support. This error is a performance optimization and not spec compliant.
2016-06-08 22:50:59.810 [info][tid:com.facebook.React.JavaScript] 'Failed to print error: ', 'One of the sources for assign has an enumerable key on the prototype chain. This is an edge case that we do not support. This error is a performance optimization and not spec compliant.'

Categories

Resources