I am trying to develop an APP in which I have to increase or decrease the user count according to their hobbies/interest in the master list. I am doing it in Node js with the help of loopback. Here is my code, in which I am giving two interests(i.e sketching and horse-riding):
async.forEach(data, function (interest) {
console.log("Interest is", interest);
Interest.findOne({
where:
{
'name': interest
}
}, function (err, interestObj) {
if (err) {
//return callback(err, null);
console.log("error", err);
}
else {
//return callback(null, response);
console.log("found", interestObj);
if (!interestObj) {
Interest.create({ "name": interest, "count": 1 }, function (err, response) { });
}
else {
_count = interestObj.count + 1;
interestObj.updateAttribute('count', _count, function (e, r) { });
}
}
});
// return callback(null, {});
},function(err){
console.log("success..!!")
});
}
but it is showing me only one of them in output. Here is output:
data is [ 'horse-riding', 'skeching' ]
Interest is horse-riding
Interest is skeching
found { name: 'horse-riding', count: 1, id: 59ccff0765055a212491a6bc }
found null
I think the async function is not working properly with forEach loop in this, But I am not getting where the code went wrong. I want to show all the interest given by the user, so what course of actions should I take to do it?? Thanks in advance..!!:)
It is working now...!!!
async.each(data, function (interest, callback2) {
console.log('Processing ', interest);
Interest.findOne({
where:
{
'name': interest
}
}, function (err, interestObj) {
if (err) {
console.log("error", err);
callback2(err);
}
else {
console.log("found", interestObj);
if (!interestObj) {
Interest.create({ "name": interest, "count": 1 }, function (err, response) { });
}
else {
_count = interestObj.count + 1;
interestObj.updateAttribute('count', _count, function (e, r) { });
}
callback2();
}
});
}, function (err) {
if (err) {
console.log('Failed to process', err);
} else {
console.log('All interests have been processed successfully');
}
return callback(err);
})
};
Related
My code is currently returning as 'undefined';
function findDoc(rowObj) {
Database.findOne(
{ 'name': rowObj.name },
'_id, foreign_id'
).exec((err, docs) => {
if (err) {
throw err;
} else {
return docs;
});
}
try {
console.log('before');
let searchDoc = await findDoc(rowObj);
console.log(searchDoc);
console.log('after');
} catch (err) {
console.log(err);
}
This outputs;
before
undefined
after
whereas if i change the findDoc function to console.log(docs); rather than return docs i get;
before
CORRECT OBJECT
after
How can i fix this to get the second result using the first structure where it returns to the main try catch block?
Thanks
You should either wrap your function with async or use a callback as follows :
function findDoc(rowObj,callback) {
Database.findOne(
{ 'name': rowObj.name },
'_id, foreign_id'
).exec((err, docs) => {
if (err) {
throw err;
} else {
callback(docs);
});
}
try {
console.log('before');
findDoc(rowObj,(searchDoc) => {
console.log(searchDoc);
console.log('after');
});
} catch (err) {
console.log(err);
}
I am using node-async-loop for asyncronous programming
var array = ['item0', 'item1', 'item2'];
asyncLoop(array, function (item, next)
{
do.some.action(item, function (err)
{
if (err)
{
next(err);
return;
}
next();
});
}, function (err)
{
if (err)
{
console.error('Error: ' + err.message);
return;
}
console.log('Finished!');
});
Like this I am using three async loops one under one.
I want to send the response only after the third inner loop ends. How can I do so?.
Here is the link for node-async-loop (https://www.npmjs.com/package/node-async-loop)
here is my code which i writing but whnever i want to response when the last loop completes it say can set header after send to cliend.
also in console log i am getting data every time when data coming from query.
const id = req.params.id;
finalData = [];
tb_user.findOne({ where: { id: id } }).then((userRiverSys, err) => {
if (userRiverSys) {
// console.log(userRiverSys.regionJson)
asyncLoop(userRiverSys.regionJson, function (item, next) {
// console.log("item", item);
tb_riverSystems.findAll(
{
where: { regionId: item.id }
}).then((findriverSys, err) => {
if (err) {
next(err);
return;
}
// console.log("findriverSys", findriverSys);
if (findriverSys) {
asyncLoop(findriverSys, function (item1, next1) {
if (err) {
next(err);
return;
}
// console.log("item1", item1.dataValues);
tb_facilities.findAll(
{
where: { riverSystemId: item1.dataValues.id }
}).then((findFacilities) => {
if (findFacilities) {
// console.log("findFacilities", findFacilities[0].dataValues.name);
asyncLoop(findFacilities, function (item2, next2) {
if (err) {
next(err);
return;
}
tb_userAccess.findAll(
{
where: { facilityId: item2.dataValues.id }
}).then((userAccessFacilities, err) => {
// console.log("userAccessFacilities", userAccessFacilities[0].dataValues);
// var i = 0;
asyncLoop(userAccessFacilities, function (item3, next3) {
finalData.push({
UserId: item3.userid,
facilityId: item3.facilityId,
})
next3();
},
function (err) {
if (err) {
console.error('Error: ' + err.message);
return;
}
// i++;
// console.log('Finished!!!!');
// if (userAccessFacilities.length === i) {
// console.log("finalData", i);
// // res.json({"status":"true", "message":"update OrgChallenge"})
// }
})
return res.json({"status":"true", "message":"update OrgChallenge"})
// console.log("finalData", finalData);
})
next2();
}, function (err) {
if (err) {
console.error('Error: ' + err.message);
return;
}
console.log('Finished!!!');
});
}
});
next1();
}, function (err) {
if (err) {
console.error('Error: ' + err.message);
return;
}
console.log('Finished!!');
});
}
});
next();
}, function (err) {
if (err) {
console.error('Error: ' + err.message);
return;
}
console.log('Finished!');
});
} else {
console.log("err3", err)
}
})
If you promisify your asynchronous action (so it returns a promise), then you can just use a regular for loop and async/await and there is no need for a 3rd party library to sequence your asynchronous loop. This is modern Javascript:
const { promisify } = require('util');
do.some.actionP = promisify(do.some.action);
async function someFunction() {
const array = ['item0', 'item1', 'item2'];
for (let item of array) {
let result = await do.some.actionP(item);
// do something with result here
}
return someFinalResult;
}
someFunction().then(result => {
console.log(result);
}).catch(err => {
console.log(err);
});
FYI, in real code, many (or even most) asynchronous operations now offer promisified versions of their API already so usually you don't even need to do the promisify step any more. For example, pretty much all databases already offer a promise interface that you can just use directly.
const loop = async (arr, results = []) => {
const item = arr.shift()
if (!item) {
console.log("DONE");
return results;
}
// as async function
await new Promise(resolve => {
resolve(results.push(`asynced-${item}`))
})
return loop(arr, results);
}
(async () => {
const result = await loop(["item0", "item1", "item2"])
console.log(result);
})();
I'd be happy if I can help you.
but this script uses a recursive function instead of node-async-loop.
so this might not be suitable for you.
I'm building a server in Nodejs to retrieve data from some database. I've worked with the async library for some time now and figured out some things like putting a waterfall inside a parallel function.
I stumbled upon a problem where I first need to execute a query and then use that query's result in other queries that can be executed concurrently. The code looks something like this:
async.waterfall([
function(callback) {
connection.query( query,
function(err, rows, fields) {
if (!err) {
callback(null,rows);
} else {
callback(null,"SORRY");
}
}
);
},
async.parallel([
function(resultFromWaterfall,callback) {
connection.query(query,
function(err, rows, fields) {
if (!err) {
callback(null,rows);
} else {
callback(null,"SORRY");
}
}
);
},
function(resultFromWaterfall,callback) {
connection.query(query,
function(err, rows, fields) {
if (!err) {
callback(null,rows);
} else {
callback(null,"SORRY");
}
}
);
}
])
], finalCallback
);
Now my problem is to access the result from the waterfall function and to use it in the parallel functions.
async.waterfall([
function(callback) {
connection.query(query,
function(err, rows, fields) {
if (!err) {
callback(null, rows);
} else {
callback(null, "SORRY");
}
}
);
},
function(prevData,callback){
console.log(prevData);//Use it whereever you want.
async.parallel([
function(callbackOfAsyncParallel) {
connection.query(query1,
function(err, rows1, fields1) {
if (!err) {
callbackOfAsyncParallel(null, rows1);
} else {
callbackOfAsyncParallel(null, "SORRY1");
}
}
);
},
function(callback) {
connection.query(query2,
function(err, rows2, fields2) {
if (!err) {
callbackOfAsyncParallel(null, rows2);
} else {
callbackOfAsyncParallel(null, "SORRY2");
}
}
);
}
],function mainCBOfParallel(err,reuslts){
if(!err){
//this will be done after tasks in async.parallel are finished.
callback(null,results);
//results[0]===>rows1
//results[1]===>rows2
}
});
}
], finalCallback);
You have two mistakes in your code,
tasks should be a function to execute.
async.parallel has only callbacks in its task functions.
Update
There are callbacks (callbackOfAsyncParallel) which will be called
when a task in async.parallel is finished.
It should not invoke callback (callback) of async.waterfall.If
done probably there may be error/unexpected results.
Try this..
async.waterfall([
function(callback) {
connection.query(query,
function(err, rows, fields) {
if (!err) {
callback(null, rows);
} else {
callback(null, "SORRY");
}
}
);
},
function(resultFromWaterfall, callback) {
async.parallel([
function() {
connection.query(query,
function(err, rows, fields) {
if (!err) {
callback(null, rows);
} else {
callback(null, "SORRY");
}
}
);
},
function() {
connection.query(query,
function(err, rows, fields) {
if (!err) {
callback(null, rows);
} else {
callback(null, "SORRY");
}
}
);
}
]);
}
], finalCallback);
doWhatever(callback) {
async.waterfall([
(waterfallCallback) => {
connection.query(query, waterfallCallback);
},
(rows, fields, waterfallCallback) => {
async.parallel([
(parallelCallback) => {
connection.query(query, parallelCallback);
},
(parallelCallback) => {
connection.query(query, parallelCallback);
}
], waterfallCallback);
}
], callback);
},
That's just me trying to make it clearer…
Anyway, my exemple is not taking into account parameters and arguments of query. It's just a scaffold
I have this async.parallel functionality inside an aysnc.eachSeries call.
I hardcoded an error so I could pass it, to see if it was behaving the way I thought. For some reason, when I pass an error, it doesn't get thrown in the final callback named "doneWithSeries".
async.eachSeries(jsonDataArr, function iterator(item, callback) {
async.parallel([
function (cb) {
if (item.hasOwnProperty('event.type')) {
var event_type = item['event.type'];
delete item['event.type'];
try {
var json = JSON.stringify(item);
}
catch (err) {
throw err;
}
fs.writeFile('./enriched_data/' + event_type + '.json', json, function (err) {
if (err) {
cb(err);
}
else {
cb(null);
}
});
}
},
function (cb) {
if (item.hasOwnProperty('status_desc')) {
var status_desc = item['status_desc'];
delete item['status_desc'];
try {
var json = JSON.stringify(item);
}
catch (err) {
throw err;
}
fs.writeFile('./enriched_data/' + status_desc + '.json', json, function (err) {
if (err) {
cb(err);
}
else {
cb(null);
}
});
}
}
],
function doneWithParallel(err) {
callback(new Error('throw this baby')); //shouldn't the first incident of error pass the error straight to the doneWithSeries callback below?
})
},
function doneWithSeries(err) {
if (err) {
throw err;
}
else {
console.log('success');
}
});
here is a distilled version of the code without anything unnecessary:
var async = require('async');
async.eachSeries(['1', '2'], function (item, callback) {
async.parallel([
function (cb) {
setTimeout(function () {
cb(null, 'one');
}, 200);
},
function (cb) {
setTimeout(function () {
cb(null, 'two');
}, 100);
}
],
function doneWithParallel(err, results) {
console.log('results', results);
callback(new Error('duh'));
})
},
function doneWithSeries(err) {
if (err)
throw err;
});
indeed that works. can't figure out why my code above doesn't, accept perhaps that the array could be empty even though when I run my code the success message gets logged...weird.
I think that's expected behavior if your list is empty. async will always call the final callback with no error even if there is no input list.
At the moment I'm running this task:
var skip = 0;
var limit = 5;
gulp.task('add coordinates to visits', function(done) {
(function recurse() {
Visit.find({})
.skip(skip)
.limit(limit)
.populate('zone')
.exec(function cb(err, visits) {
if (err) {
throw err;
}
if (visits.length === 0) {
return;
}
async.each(visits, function iterateEvents(visit, next) {
if (!visit.zone) {
return next();
} else if (!visit.coordinates.lat || !visit.coordinates.lng) {
visit.coordinates = {
lat: visit.zone.geo.coordinates.lat,
lng: visit.zone.geo.coordinates.lng
};
}
visit.save(next);
}, function cb(err) {
if (err) {
throw err;
}
skip += limit;
setTimeout(recurse, 1000);
});
});
})();
});
But I'm sure there must be a more elegant and optimal method than using skip, limit, `setTimeout. Is there some mongo or mongoose method for running updating tasks?
Based on our conversation in the comments it seems like Mongoose's querystream might be what you are looking for:
var stream = Visits.find().populate('zone').stream();
stream.on('data', function processDoc(visit) {
var self = this;
if (visit.zone && (!visit.coordinates.lat || !visit.coordinates.lng)) {
self.pause();
visit.update({
coordinates: {
lat: visit.zone.geo.coordinates.lat,
lng: visit.zone.geo.coordinates.lng
}
}, function(err, result) {
if (err) { console.log(err); };
self.resume();
});
}
});
stream.on('error', function(err) {
console.log('error', err);
});
stream.on('close', function() {
console.log('closed');
});