wait for function and then console result - node.js

I want to wait for my functions like fetching all result first, then consoling my result.
exports.listofAllFeaturedProd = (req, res) => {
Product.find({is_featured:'1'},function(error,fetchallFeatProds)
{
var allProducts = new Array();
var pp = 0;
var products = new Array();
for (var ProductId in fetchallFeatProds)
{
var pArr = [];
pArr['_id'] = fetchallFeatProds[ProductId]._id;
pArr['name'] = fetchallFeatProds[ProductId].name;
pArr['sku'] = fetchallFeatProds[ProductId].sku;
pArr['description'] = fetchallFeatProds[ProductId].description;
pArr['price'] = fetchallFeatProds[ProductId].price;
pArr['large_image'] = fetchingImage(fetchallFeatProds[ProductId]._id);
pArr['brand'] = fetchingBrand(fetchallFeatProds[ProductId].brand_id);
console.log('#################### IMAGE ####################');
console.log(pArr);
pp++;
}
console.log(products);
});
};
function fetchingImage(pid)
{
ProductImage.findOne({product_id:pid},function(error,fetchallFeatProdsImgs)
{
console.log(fetchallFeatProdsImgs.large_image);
return fetchallFeatProdsImgs.large_image;
});
}
function fetchingBrand(bid)
{
Brand.findOne({_id:bid},function(error,fetchAllBrands)
{
console.log(fetchAllBrands);
return fetchAllBrands;
});
}
node not wait for functions and console undefined after that console my function result. how i stop my code for fetching first result then console all data in array.
Output for console.log(pArr);
[ _id: 57bd996ebf8c930b2bcc06a1,
name: 'New Product',
sku: 'New-Product',
description: 'New Product',
price: 'test',
large_image: undefined,
brand: undefined ]
After that added console inside functions which gave results as below:
Output for fetchingImage
images/12.png
Output for fetchingBrand
{ user_id: '57b42b571fc35e49162de413',
brand_name: '10 Fork ',
brand_logo: 'uploads/brands_logo/1472027911329_5.png',
brand_desc: '10 Fork',
_id: 57bd5ce6cebed2a3189cedcf,
__v: 0 }
Desired output is:
[ _id: 57bd996ebf8c930b2bcc06a1,
name: 'New Product',
sku: 'New-Product',
description: 'New Product',
price: 'test',
large_image: images/12.png,
brand: { user_id: '57b42b571fc35e49162de413',
brand_name: '10 Fork ',
brand_logo: 'uploads/brands_logo/1472027911329_5.png',
brand_desc: '10 Fork',
_id: 57bd5ce6cebed2a3189cedcf,
__v: 0 } ]

Try below code:
var temp = [],
async = require('async');
async.eachSeries(fetchallFeatProds, function(ProductId, callback)
{
pArr['_id'] = ProductId._id;
pArr['name'] = ProductId.name;
pArr['sku'] = ProductId.sku;
pArr['description'] = ProductId.description;
pArr['price'] = ProductId.price;
pArr['large_image'] = fetchingImage(ProductId._id);
pArr['brand'] = fetchingBrand(ProductId.brand_id);
temp.push(pArr);
callback(null);
}, function(err){
console.log(temp); //This should give you desired result
});
If it is not working still, try using callbackfor the functions fetchingImage and fetchingBrand. Or you may try using async.parallel as well inside eachSeries.
EDIT:-
async-eachseries
Change your functions with callback.
function fetchingImage(pid, callback)
{
ProductImage.findOne({product_id:pid},function(error,fetchallFeatProdsImgs)
{
console.log(fetchallFeatProdsImgs.large_image);
callback(error,fetchallFeatProdsImgs.large_image);
});
}
function fetchingBrand(bid, callback)
{
Brand.findOne({_id:bid},function(error,fetchAllBrands)
{
console.log(fetchAllBrands);
calback(error,fetchAllBrands);
});
}
Use async.parallel so that it will wait till both the functions are done. Then push into temp array. Doc to refer
async.eachSeries(fetchallFeatProds, function(ProductId, callback)
{
var pArr = {};
pArr['_id'] = ProductId._id;
pArr['name'] = ProductId.name;
pArr['sku'] = ProductId.sku;
pArr['description'] = ProductId.description;
pArr['price'] = ProductId.price;
async.parallel([
function(callback)
{
fetchingImage(ProductId._id, function(err, res){
pArr['large_image'] = res;
callback(err); //Forgot to add
});
},
function(callback)
{
fetchingBrand(ProductId.brand_id,function(err, res){
pArr['brand'] = res;
callback(err); //Forgot to add
});
},
], function(err){
console.log(pArr); //Edit
temp.push(pArr);
callback(err);
})
}, function(err){
console.log(temp); //This should give you desired result
callback(err);
});

Related

Remove object array items by comparing object array from mongodb

There is document which is having array of object inside.
Like
Objectid('')
fruits : [{_id:'2'},{_id:'3'},{_id:'4'}]
I want to delete these items fruits_id = [{_id:'3'},{_id:'4'}].
fruit_group.updateOne(
{collection_id: collection_id},
{$pullAll: {"fruits": fruits_id}}
)
so far i found below logic which i think is not efficient.
routes.post('/removeFruits', async (request, response, next) => {
var post_data = request.body;
var collection_id = post_data.collection_id;
var fruits_ids = JSON.parse(post_data.fruits_ids);
var prev_fruits;
await fruit_group.findOne({'collection_id': collection_id}, function (err, result) {
if (err) {
console("Some error occurred");
response.json({'message': "Some error occurred", 'result': 'false'});
}
prev_fruits = result.fruits;
});
for (var i = 0; i < fruits_ids.length; i++) { // this will delete all occurring items from array
var key = fruits_ids[i].user_id;
prev_fruits.filter(x => x.user_id === key).forEach(x => prev_fruits.splice(prev_fruits.indexOf(x), 1));
}
await fruit_group.updateOne({'collection_id': collection_id}, {$set: {'fruits': prev_fruits}}, function (err, result) {
if (err) {
response.json({'message': "Some error occurred", 'result': 'false'});
}
response.json({'message': 'Deletion successfully', 'result': 'true'});
});
});
is there anyway to achieve the same result?
Assuming fruits_id = [{ _id: '3' }, { _id: '4' }], you could do something like this using $pull and $in:
await fruit_group.updateOne({'collection_id': collection_id}, { $pull: { fruits: { $in: fruits_id }}})
This follows the example of removing all items that equal a specified value.

nodejs - test failing but callback being called

I have a module which I export and which has a method editHeroImage which I am trying to test using mocha, chai and sinon. The modules has two objects that are passed as arguments, connection and queries. These are mySql objects, one containing the connection to the database and the other the query strings which are defined in their separate modules. The expObj which I am exporting and trying to test is a "helper" module.
I have successfully tested other methods of this module in the same way I am trying to test this method, but, however when I run into methods which use the async module for some reason, my tests no longer behave as expected. I wonder if I am missing something in this particular case, because I have tested other modules and methods which also use async and have not come across this behaviour.
When I run the tests, it logs "HELLO!" as expected but the assertion that the callbackSpy has been called, fails.
I am losing my mind here! Please help! What is going on? Could there be contamination between test suits?
Method under test:
expObj.editHeroImage = function(connection, queries, postId, postData, callback) {
async.waterfall([
function(next) {
var qString = queries.getSinglePostById();
connection.query(qString, [postId], function(err, results) {
if (err) {
return next(err);
}
if (!results.length) {
console.log('NO POST FOUND WITH ID ' + postId);
return callback();
}
next(null, results[0].hero_image);
});
},
function(heroImageId, next) {
if (!heroImageId) {
console.log('HERO IMAGE IS NEW - NEXT TICK!');
return next();
}
// Delete resized images of hero image
var queryStr = queries.deleteResizedImages();
var resizedVals = [heroImageId];
connection.query(queryStr, resizedVals, function(err) {
if (err) {
return callback(err);
}
console.log('DELETED RESIZED IMAGES OF HERO IMAGE ' + heroImageId);
var qString = queries.updateHeroImagePath();
var values = [postData.hero_image, heroImageId];
return connection.query(qString, values, function(err, results) {
if (err) {
return next(err);
}
console.log('UPDATED HERO IMAGE ' + heroImageId + ' WITH PATH ' + postData.hero_image);
next('break');
});
});
},
function addHeroImage(next) {
var qString = queries.insertImage();
var values = [postData.hero_image, postId];
connection.query(qString, values, function(err, results) {
if (err) {
return next(err);
}
next(null, results.insertId);
});
},
function addHeroImagePathToPost(heroImageId, next) {
var qString = queries.saveHeroImageId();
var values = [heroImageId, postId];
connection.query(qString, values, function(err) {
if (err) {
return next(err);
}
next();
});
}
], function(err) {
if (err && err !== 'break') {
return callback(err);
}
console.log('HELLO!');
callback(null);
});
};
Test, with set-up:
'use strict';
var chai = require('chai');
var sinonChai = require("sinon-chai");
var proxyquire = require('proxyquire');
var sinon = require('sinon');
chai.use(sinonChai);
var expect = chai.expect;
describe('HELPERS', function() {
var testedModule,
callbackSpy,
fakeConnectionObj,
fakeQueriesObj,
fakePost,
fakeSnakeCaseObj,
queryStub,
connectionStub,
manageStub,
fakeCamelCaseObj;
beforeEach(function() {
fakePost = {};
fakeConnectionObj = {};
fakeQueriesObj = {
getPostIdFromImage: function() {},
insertResizedImages: function() {},
createPost: function() {},
getPostImages: function() {},
getPostsAlternativesImages: function() {},
getSinglePostById: function() {},
getAllImages: function() {},
insertImage: function() {},
deleteMainImage: function() {},
deleteResizedImages: function() {},
updateHeroImagePath: function() {},
saveHeroImageId: function() {}
};
afterEach(function() {
queryStub.resetBehavior();
});
fakeSnakeCaseObj = {
sub_title: '123',
hero_image: '456'
};
fakeCamelCaseObj = {
subTitle: '123',
heroImage: '456'
};
callbackSpy = sinon.spy();
queryStub = sinon.stub();
manageStub = sinon.stub();
connectionStub = {query: queryStub};
testedModule = proxyquire('./../../../../lib/modules/mySql/workers/helpers', {
'./../../../factories/notification-service': {
select: function() {
return {manageSns: manageStub};
}
}
});
});
it('edits hero image', function() {
var _post = {
id: '123',
title: 'vf',
sub_title: 'vf',
slug: 'vf',
reading_time: 4,
created_at: '123',
published_at: '123',
deleted_on: false,
hero_image: 'hero_image_path'
};
var _postId = '123';
queryStub.onCall(0).callsArgWith(2, null, [{hero_image: '55'}]);
queryStub.onCall(1).callsArgWith(2, null);
queryStub.onCall(2).callsArgWith(2, null);
testedModule.editHeroImage(connectionStub, fakeQueriesObj, _postId, _post, function() {
console.log(arguments); // --> {'0': null} as expected
callbackSpy.apply(null, arguments);
});
expect(callbackSpy).has.been.calledWith(null);
});
});
Your assertion is probably executing before your async function has returned.
There are a number of ways to ensure your async functions have finished executing. The cleanest is to format your mocha test differently.
describe('...', function () {
var callbackSpy;
before(function () {
var _post = {
id: '123',
title: 'vf',
sub_title: 'vf',
slug: 'vf',
reading_time: 4,
created_at: '123',
published_at: '123',
deleted_on: false,
hero_image: 'hero_image_path'
};
var _postId = '123';
queryStub.onCall(0).callsArgWith(2, null, [{
hero_image: '55'
}]);
queryStub.onCall(1).callsArgWith(2, null);
queryStub.onCall(2).callsArgWith(2, null);
return testedModule.editHeroImage(connectionStub, fakeQueriesObj, _postId, _post, function () {
console.log(arguments); // --> {'0': null} as expected
callbackSpy.apply(null, arguments);
});
});
it('edits hero image', function () {
expect(callbackSpy).has.been.calledWith(null);
});
});
Notice that I have wrapped your assertion in a describe block so we can use before. Your actual logic for setting up stubs and executing the class has been moved to the before block and a return added, this ensures the async function is complete before moving on to your assertions.
Your other tests may have passed, but they will also be susceptible to this and it is purely a timing issue.
Indeed #Varedis was right about it being a timing issue. However using your suggestion of wrapping the assertion in a describe bloack and using the before function to set-up the test resulted in my stubs no longer working correctly. However taking your suggestion about timing into account I managed to solve the issue by using the done callback within my test suit. By keeping the set-up I made a slight change and my tests suddenly passed:
it('edits hero image', function(done) {
var _post = {
id: '123',
title: 'vf',
sub_title: 'vf',
slug: 'vf',
reading_time: 4,
created_at: '123',
published_at: '123',
deleted_on: false,
hero_image: 'hero_image_path'
};
var _postId = '123';
queryStub.onCall(0).callsArgWith(2, null, [{hero_image: '55'}]);
queryStub.onCall(1).callsArgWith(2, null);
queryStub.onCall(2).callsArgWith(2, null);
testedModule.editHeroImage(connectionStub, fakeQueriesObj, _postId, _post, function() {
callbackSpy.apply(null, arguments);
expect(callbackSpy).has.been.calledWith(null);
expect(callbackSpy).has.not.been.calledWith('FDgdjghg');
done();
});
});

async each is running to fast?

I have the following code running through an object with async:
async.each(Object.keys(shopList), function(key, callback){
var shop = shopList[key];
saveOrder(payId, shopList[key], key, req.body, req.user, function(err, newOrder){
if (err) {
callback(err);
}else{
orderCount++;
console.log("succes!", orderCount, newOrder.number);
callback();
}
})
}, function(err){
if (err) {
console.log("ERROR!", err);
}else{
console.log("done!");
}
})
In this function a another function is called. This code looks like this:
saveOrder = function(payId, shop, nameSlug, body, user, callback){
console.log("saveOrder");
var orderNumber = 0;
Order.findOne().sort({_id:-1}).exec(function(err, latestOrder) {
if(latestOrder.number){
orderNumber = latestOrder.number.split("-")[1];
}
var order = new Order();
var date = new Date();
order.number = date.getFullYear().toString() + date.getMonth().toString() + "-" + (parseInt(orderNumber)+1);
order.date = date;
order.payId = payId;
order.status = {
status: "Created",
comment: "",
date: new Date()
};
order.comment = body.comment;
order.shop = {
name: shop.name,
nameSlug: nameSlug
}
order.billingDetails = {
//order details
}
order.sendDetails = {
//more order details
}
order.user = {
//yep, order details
}
var orderItems = [];
for(p = 0; p < shop.items.length; p++){
var product = shop.items[p];
var orderItem = {
_id: product._id,
name: product.name,
brand: product.brand[0].name,
price: product.price,
quantity: product.quantity
}
orderItems.push(orderItem);
}
order.items = orderItems;
order.save(function(err, result){
if (err){
console.log("err!", err);
return callback(err)
}else{
console.log("saved!");
return callback(null, result);
}
})
})
}
The problem is in the last function. There I try to create a ordernumber which must be unique. I get the last order, split the ordernumber and do a +1.
When I have more objects in my shopList, this function is triggered when he is not ready. With other words, the first order isn't saved then, and I will get the same ordernumber.
How can I fix this? I tried a setTimeout in the async.each but that isn't working.
You could use a mutex using locks.
The callbacks will wait that the mutex is unlocked to lock it making that you won't have simultaneous executions.
var locks = require('locks');
var mutex = locks.createMutex();
saveOrder = function(payId, shop, nameSlug, body, user, callback){
mutex.lock(function () {
console.log("saveOrder");
var orderNumber = 0;
Order.findOne().sort({_id:-1}).exec(function(err, latestOrder) {
if(latestOrder.number){
orderNumber = latestOrder.number.split("-")[1];
}
var order = new Order();
var date = new Date();
order.number = date.getFullYear().toString() + date.getMonth().toString() + "-" + (parseInt(orderNumber)+1);
order.date = date;
order.payId = payId;
order.status = {
status: "Created",
comment: "",
date: new Date()
};
order.comment = body.comment;
order.shop = {
name: shop.name,
nameSlug: nameSlug
}
order.billingDetails = {
//order details
}
order.sendDetails = {
//more order details
}
order.user = {
//yep, order details
}
var orderItems = [];
for(p = 0; p < shop.items.length; p++){
var product = shop.items[p];
var orderItem = {
_id: product._id,
name: product.name,
brand: product.brand[0].name,
price: product.price,
quantity: product.quantity
}
orderItems.push(orderItem);
}
order.items = orderItems;
order.save(function(err, result){
if (err){
console.log("err!", err);
return callback(err)
}else{
console.log("saved!");
return callback(null, result);
}
})
})
mutex.unlock(); //don't forget to unlock the mutex
});
}
You should use async.waterfall instead of async.each, because:
async.waterfall - runs the tasks array of functions in series, each passing their results to the next in the array. http://caolan.github.io/async/docs.html#waterfall
async.each - applies the function iteratee to each item in coll, in parallel.
Fixed this issue with using eachSeries() instead of each()
http://caolan.github.io/async/docs.html#eachSeries

Jade template doesn't get the data passed from express

I know this questions has almost the same title but the issue is different.
I'm using Jade template engine v.1.11.0 built into latest Keystone.js release. In a controller, I query the data with two view.on('init') callbacks. First callback only queries one record and always passes. The second sometimes don't.
category.js
var keystone = require('keystone');
var async = require('async');
exports = module.exports = function (req, res) {
var view = new keystone.View(req, res);
var locals = res.locals;
// Init locals
locals.section = 'category';
locals.filters = {
category: req.params.category
};
locals.data = {
sections: [],
category: {}
};
// Load current category
view.on('init', function (next) {
var q = keystone.list('Category').model.findOne({
key: locals.filters.category
});
q.exec(function (err, result) {
locals.data.category = result;
locals.section = locals.data.category.name.toLowerCase();
next(err);
});
});
// Load sections
view.on('init', function (next) {
var q = keystone.list('Section').model.find().where('category').in([locals.data.category]).sort('sortOrder').exec(function(err, results) {
if (err || !results.length) {
return next(err);
}
async.each(results, function(section, next) {
keystone.list('Article').model.find().where('section').in([section.id]).sort('sortOrder').exec(function(err, articles){
var s = section;
if (articles.length) {
s.articles = articles;
locals.data.sections.push(s);
} else {
locals.data.sections.push(s);
}
});
}, function(err) {
console.log(locals.data.sections);
});
next(err);
});
});
view.render('category');
};
In my view, I should always get this passed:
sections: { _id: 574b909b43ff68163ed86bf2, publicTitle: 'Title 1', key: 'name-1', sortOrder: 3, name: 'Name 1', __v: 0, category: 574b8960947f45f034ac89b4, text: '', image: {} }
category: { _id: 574b8960947f45f034ac89b4, key: 'blabla', sortOrder: 1, name: 'Blabla', __v: 0, image: {} }
But 60% of the time, I get this:
sections:
category: { _id: 574b8960947f45f034ac89b4, key: 'johndoe', sortOrder: 1, name: 'JohnDoe', __v: 0, image: {} }
Strange thing is, If I go to another category, which has more sections and like 30 articles, I get sections 90% of the time, but still missing them 10%. This persists in both development and production.
Is some "next()" firing too early? I can't see where I messed up.
Alright. After some reading on async I managed to get it to work properly using async.parallel.
var keystone = require('keystone');
var async = require('async');
exports = module.exports = function (req, res) {
var view = new keystone.View(req, res);
var locals = res.locals;
// Init locals
locals.section = 'category';
locals.filters = {
category: req.params.category
};
locals.data = {
sections: [],
category: {}
};
// Load current category
view.on('init', function (next) {
var q = keystone.list('Category').model.findOne({
key: locals.filters.category
});
q.exec(function (err, result) {
locals.data.category = result;
locals.section = locals.data.category.name.toLowerCase();
getChildrenRecords(locals.data.category, next);
});
});
function getChildrenRecords(category, next){
var q = keystone.list('Section').model.find().where('category').in([category]).sort('sortOrder').exec(function(err, results) {
if (err || !results.length) {
return next(err);
}
callItems = [];
for(var i = 0; i < results.length; i++) {
var data = results[i];
callItems.push(makeCallbackFunction(data));
}
function makeCallbackFunction(section) {
return function (callback) {
keystone.list('Article').model.find().where('section').in([section.id]).sort('sortOrder').exec(function(err, articles){
if (err) return callback(err);
if (articles.length) { section.articles = articles; }
locals.data.sections.push(section);
callback();
});
};
}
async.parallel(callItems, function(err, results){
next(err);
});
});
}
// Render the view
view.render('category');
};

node js mongo db dependencies (doc not being found)

I have the following code:
var method = PushLoop.prototype;
var agent = require('./_header')
var request = require('request');
var User = require('../models/user_model.js');
var Message = require('../models/message_model.js');
var async = require('async')
function PushLoop() {};
method.startPushLoop = function() {
getUserList()
function getUserList() {
User.find({}, function(err, users) {
if (err) throw err;
if (users.length > 0) {
getUserMessages(users)
} else {
setTimeout(getUserList, 3000)
}
});
}
function getUserMessages(users) {
// console.log("getUserMessages")
async.eachSeries(users, function (user, callback) {
var params = {
email: user.email,
pwd: user.password,
token: user.device_token
}
messageRequest(params)
callback();
}, function (err) {
if (err) {
console.log(err)
setTimeout(getUserList, 3000)
}
});
}
function messageRequest(params) {
var url = "https://voip.ms/api/v1/rest.php?api_username="+ params.email +"&api_password="+ params.pwd +"&method=getSMS&type=1&limit=5"
request(url, function(err, response, body){
if (!err) {
var responseObject = JSON.parse(body);
var messages = responseObject.sms
if (responseObject["status"] == "success") {
async.eachSeries(messages, function(message, callback){
console.log(params.token)
saveMessage(message, params.token)
callback();
}, function(err) {
if (err) {
console.log(err)
}
// setTimeout(getUserList, 3000)
})
} else {
// setTimeout(getUserList, 3000)
}
} else {
console.log(err)
// setTimeout(getUserList, 3000)
}
});
setTimeout(getUserList, 3000)
}
function saveMessage(message, token) {
// { $and: [ { price: { $ne: 1.99 } }, { price: { $exists: true } }
// Message.find({ $and: [{ message_id: message.id}, {device_token: token}]}, function (err, doc){
Message.findOne({message_id: message.id}, function (err, doc){
if (!doc) {
console.log('emtpy today')
var m = new Message({
message_id: message.id,
did: message.did,
contact: message.contact,
message: message.message,
date: message.date,
created_at: new Date().toLocaleString(),
updated_at: new Date().toLocaleString(),
device_token: token
});
m.save(function(e) {
if (e) {
console.log(e)
} else {
agent.createMessage()
.device(token)
.alert(message.message)
.set('contact', message.contact)
.set('did', message.did)
.set('id', message.id)
.set('date', message.date)
.set('message', message.message)
.send();
}
});
}
}) //.limit(1);
}
};
module.exports = PushLoop;
Which actually works perfectly fine in my development environment - However in production (i'm using Openshift) the mongo documents get saved in an endless loop so it looks like the (if (!doc)) condition always return true therefore the document gets created each time. Not sure if this could be a mongoose issue - I also tried the "find" method instead of "findOne". My dev env has node 0.12.7 and Openshift has 0.10.x - this could be the issue, and i'm still investigating - but if anybody can spot an error I cannot see in my logic/code please let me know
thanks!
I solved this issue by using a "series" like pattern and using the shift method on the users array. The mongoose upsert findOneOrCreate is good however if there is a found document, the document is returned, if one isn't found and therefore created, it's also returned. Therefore I could not distinguish between the newly insert doc vs. a found doc, so used the same findOne function which returns null if no doc is found I just create it and send the push notification. Still abit ugly, and I know I could have used promises or the async lib, might refactor in the future. This works for now
function PushLoop() {};
var results = [];
method.go = function() {
var userArr = [];
startLoop()
function startLoop() {
User.find({},function(err, users) {
if (err) throw err;
users.forEach(function(u) {
userArr.push(u)
})
function async(arg, callback) {
var url = "https://voip.ms/api/v1/rest.php?api_username="+ arg.email +"&api_password="+ arg.password +"&method=getSMS&type=1&limit=5"
request.get(url, {timeout: 30000}, function(err, response, body){
if (!err) {
var responseObject = JSON.parse(body);
var messages = responseObject.sms
var status = responseObject.status
if (status === "success") {
messages.forEach(function(m) {
var message = new Message({
message_id: m.id,
did: m.did,
contact: m.contact,
message: m.message,
date: m.date,
created_at: new Date().toLocaleString(),
updated_at: new Date().toLocaleString(),
device_token: arg.device_token
});
var query = { $and : [{message_id: m.id}, {device_token: arg.device_token}] }
var query1 = { message_id: m.id }
Message.findOne(query).lean().exec(function (err, doc){
if (!doc || doc == null) {
message.save(function(e) {
console.log("message saved")
if (e) {
console.log("there is an error")
console.log(e)
} else {
console.log(message.device_token)
var messageStringCleaned = message.message.toString().replace(/\\/g,"");
var payload = {
"contact" : message.contact,
"did" : message.did,
"id" : message.message_id,
"date" : message.date,
"message" : messageStringCleaned
}
var note = new apns.Notification();
var myDevice = new apns.Device(message.device_token);
note.expiry = Math.floor(Date.now() / 1000) + 3600; // Expires 1 hour from now.
note.badge = 3;
note.alert = messageStringCleaned;
note.payload = payload;
apnsConnection.pushNotification(note, myDevice);
}
})
}
});
});
}
else {
console.log(err)
}
}
});
setTimeout(function() {
callback(arg + "testing 12");
}, 1000);
}
// Final task (same in all the examples)
function series(item) {
if(item) {
async( item, function(result) {
results.push(result);
return series(userArr.shift());
});
} else {
return final();
}
}
function final() {
console.log('Done');
startLoop();
}
series(userArr.shift())
});
}
}
module.exports = PushLoop;

Resources