NodeJS - Events.js cannot read property forEach of undefined - javascript

I'm facing an issue since 2 days and i can't figure out how to fix it. I've an error on forEach, so my application runs well and then stops without any explication.
Here is the code where the error happens.
var easy = setInterval(function(){
keywords.forEach(function(k) {
tweetModel.find({keyword: k}).sort({date: -1}).limit(20).exec(function(err, data) {
var score = [];
var date = [];
console.log(data);
console.log(err)
data.forEach(function (item) {
score.push(Math.floor(parseFloat(item.score) * 1000) / 1000);
date.push(item.date.getDate()+'/'+parseInt(item.date.getMonth() + 1)+'/'+item.date.getFullYear()+':'+parseInt(item.date.getHours() + 1)+':'+item.date.getMinutes());
tArrayStats[k] = score;
tArrayStats['date'] = date;
});
});
});
io.sockets.emit('stats',tArrayStats);
},3000);
The error is thrown here
data.forEach(function (item)
but i can't figure out why ! Thanks for you help.
As asked there is the output of console log data :
EDITED working code, thanks to #Ids van der Zee
var easy = setInterval(function(){
keywords.forEach(function(k) {
tweetModel.find({keyword: k}).sort({date: -1}).limit(20).exec(function(err, data) {
if (data && !err)
{
var score = [];
var date = [];
console.log(data);
console.log(err)
data.forEach(function (item) {
score.push(Math.floor(parseFloat(item.score) * 1000) / 1000);
date.push(item.date.getDate()+'/'+parseInt(item.date.getMonth() + 1)+'/'+item.date.getFullYear()+':'+parseInt(item.date.getHours() + 1)+':'+item.date.getMinutes());
tArrayStats[k] = score;
tArrayStats['date'] = date;
});
}
});
});
io.sockets.emit('stats',tArrayStats);
},3000);

on the line:
tweetModel.find({keyword: k}).sort({date: -1}).limit(20).exec(function(err, data) {
you are trying to find data corresponding to the keyword k, you are doing this for each keyword. If tweetModel does not contain the keyword you are looking for the data variable will be undefined. You can resolve this by checking if data is not undefined. Instead of
data.forEach(function (item){...
if(data){
data.forEach(function (item){...

Related

How to trigger firebase http function in node.js?

I am trying to trigger an another function in Firebase Cloud function with javascript. But i always getting an error of Can't set headers after they are sent. Please take a look at my code below: ................. ................. ............ ................ ................. ............... ....................... .................. ..............
exports.productIndexShuffleOne = functions.https.onRequest(async (req, res) => {
const interval = req.query.interval;
console.log("interval: "+interval);
const productRef = admin.firestore().collection("Products");
const adminRef = admin.firestore().collection("Admin").doc("totalProd").get();
const dateRef = admin.firestore().collection("Admin").doc("totalProd").collection("indexShuffle").doc("productShuffle").get();
return dateRef.then(documentSnapshot => {
const setDate = documentSnapshot.get('date').seconds;
var nextDay = setDate;
console.log("Date: "+nextDay);
const x = setInterval(function() {
clearInterval(x);
return Promise.all([adminRef]).then(result => {
const totalNum = result[0].data().totalNumber;
console.log("totalNum: "+totalNum);
var numberList = [];
var index = 1;
while(index <= totalNum){
numberList.push(index);
index++;
}
var cidx, ridx, tmp;
cidx = numberList.length;
while (cidx !== 0) {
ridx = Math.floor(Math.random() * cidx);
cidx--;
tmp = numberList[cidx];
numberList[cidx] = numberList[ridx];
numberList[ridx] = tmp;
}
console.log(numberList);
var counter = 0;
return productRef.get().then(snapshot => {
snapshot.forEach(doc => {
const prodID = doc.get('productID');
const index = doc.get('index');
var newIndex = numberList[counter];
counter++;
console.log("oldIndex: "+index);
console.log("newIndex: "+newIndex);
productRef.doc(prodID).update({
index: newIndex
}, {merge: true});
});
return res.redirect('https://us-central1-myfunction-123456.cloudfunctions.net/productIndexShuffleTwo?interval='+interval);
})
.catch(err => {
console.log('Error getting documents', err);
});
});
}, interval);
return res.status(203).send(interval);
}).catch(function(err) {
console.error(err);
});
});
This is because you've sent multiple responses while the rule is that you only allowed sending one response. Please try to look at your code and optimize it in such a way that it contains only one response.
I can see you have multiple responses as below:
1 -> return res.redirect('https://us-central1-myfunction-123456.cloudfunctions.net/productIndexShuffleTwo?interval='+interval);
2 -> return res.status(203).send(interval);
I believe that you can have res.redirect and then res.status.send called one after another. When you writing endpoints there rule of a thumb: always send response and only do that once. Refactor your code so there no way you can make those two calls, but only one of them.

Passing function in other functions as parameters

I've recently started studying programming and JS, HTML, CSS.
Reading a book at the moment which includes the following snippet of code which i try to understand and modify for my own practice and attempts in understanding. But i really can't grasp what's going on.
Is there anyone that could please try and explain what's happening and why my modified snippet of code won't run - as it does look similar to part of the original one that does run fine.
First snippet attached is original one from book.
Second is mine which is built on parts of the prior one.
var validateDataForAge = function(data) {
person = data();
console.log(person);
if (person.age <1 || person.age > 99){
return true;
} else{
return false;
}
};
var errorHandlerForAge = function(error) {
console.log("Error while processing age");
};
function parseRequest(data,validateData,errorHandler) {
var error = validateData(data);
if (!error) {
console.log("no errors");
} else {
errorHandler();
}
}
var generateDataForScientist = function() {
return {
name: "Albert Einstein",
age : Math.floor(Math.random() * (100 - 1)) + 1,
};
};
//parse request
parseRequest(generateDataForScientist, validateDataForAge,
errorHandlerForAge);
var validateAge = function(age) {
person = age();
console.log(age);
}
validateAge(17);
I get following errormessage:
TypeError: age is not a function
at validateAge:2:12
at eval:7:1
at eval
at new Promise
Thankful for any help.
Regards,
Here is the code you are looking at. It expects data to be a function.
var validateDataForAge = function(data) {
person = data();
… and so it is (the one assigned to generateDataForScientist after it gets passed through a couple of other variables and function calls).
Here is your code:
var validateAge = function(age) {
person = age();
It expects age to be a function.
Here you pass it a value:
validateAge(17);
17 is a number, not a function.
The problem is here in this part of your code
var generateDataForScientist = function() {
return {
name: "Albert Einstein",
age : Math.floor(Math.random() * (100 - 1)) + 1, // <- problem
};
};
age is just a property of the object your returning and you are using age as a function in this code
var validateAge = function(age) {
person = age(); // <-- here ,age is not a function
console.log(age);
}
you need to chage the age property to be a function like this
var generateDataForScientist = function() {
return {
name: "Albert Einstein",
age : function (){ return Math.floor(Math.random() * (100 - 1)) + 1 },
};
};
consoling data inside parseRequest will log it as a function. But you need the property age. So replace validateData(data) with validateData(data());
var validateDataForAge = function(data) {
let person = data;
if (person.age < 1 || person.age > 99) {
return true;
} else {
return false;
}
};
var errorHandlerForAge = function(error) {
console.log("Error while processing age");
};
function parseRequest(data, validateData, errorHandler) {
console.log(data)
var error = validateData(data());
if (!error) {
console.log(error);
} else {
errorHandler();
}
}
var generateDataForScientist = function() {
return {
name: "Albert Einstein",
age: Math.floor(Math.random() * (100 - 1)) + 1,
};
};
//parse request
parseRequest(generateDataForScientist, validateDataForAge, errorHandlerForAge);

Braintree Transaction.search in Meteor Server

How can I wait for the Braintree Transaction.search() function to return all data.
Right now it does not wait and just comes back with undefined return value.
Here is the code
I tried to use Meteor.asynwrap but that also does not work.
`
function getTrxns(cid) {
var future = new Future();
var trxns = [];
var i = 0
var stream = gateway.transaction.search(function (search) {
r = search.customerId().is(cid)});
stream.on("data", function(data){
i = i+1
trxns.push({
'id':data.id,
'amount':data.amount,
'crtDt': data.createdAt,
'ccType': data.creditCard.cardType,
'currency': data.currencyIsoCode,
'last4': data.creditCard.last4,
'expdt': data.creditCard.expirationDate
});
});
stream.on("end", function(){
// print the output in console
console.log('End Stream cnt: '+i);
return trxns;
});
stream.resume();
}
Meteor.methods({
findCustTrxns: function() {
var btId = Meteor.user().custBtId;
if (!btId) { return []; };
console.log('findCustTrxns cusBtId: '+btId);
var xx = getTrxns(btId);
console.log('xx len :'+xx.length);
}
});
OUTPUT is:
I20170509-15:22:09.095(0)? findCustTrxns cusBtId: 232057823
I20170509-15:22:09.095(0)? Exception while invoking method 'findCustTrxns' TypeError: Cannot read property 'length' of undefined
I20170509-15:22:09.095(0)? End Stream cnt: 56
Found a way to make it work:
1. Added a callback function
function getTrxns(cid,callback )
2. invoked the callback in stream.on('end;..) Here is the code
function getTrxns(cid,callback ) {
var trxns = [];
var i = 0
var stream = gateway.transaction.search(function (search) {
r = search.customerId().is(cid)});
stream.on("data", function(data){
i = i+1
trxns.push({
'id':data.id,
});
});
stream.on("end", function(){
// print the output in console
console.log('End Stream cnt: '+i);
callback('', trxns);
});
stream.resume();
}
3. Changed the Meteor Method :
findCustTrxns: function(btId) {
if (!btId) { return []; };
console.log('findCustTrxns cusBtId: '+btId);
var trxns = [];
var i = 0;
var fn = Meteor.wrapAsync(getTrxns); //made it a synchronous call
try {
var res = fn(btId);
if (res) {
console.log('Got data from getTrxns ');
return res;
}
} catch( err) {
console.log('Error calling hetTrxns '+err);
}
}, //findCustTrxns
Now I am able to get the Transactions. Hope it helps

Node.js Function Flow

When I get a request, I want it to generate a 4-character code, then check if it already exists in the database. If it does, then generate a new code. If not, add it and move on. This is what I have so far:
var code = "";
var codeFree = false;
while (! codeFree) {
var chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
var code = "";
for (var i = 0; i < 4; i++) {
var rand = Math.floor(Math.random() * chars.length);
console.log(rand);
code += chars.charAt(rand);
}
console.log("Code: %s generated.", code);
client.execute("select * from codes where code=" + code, function(err, result) {
if (! err) {
if (result.rows.length > 0) {
codeFree = false;
} else {
codeFree = true;
}
} else {
console.log('DB ERR: %s', err);
}
console.log(codeFree);
});
console.log('here');
}
This does not do nearly what I want it to do. How can I handle something like this?
You are doing an async task.
When you have an asyncronous task inside your procedure, you need to have a callback function which is going to be called with the desired value as its argument.
When you found the free code, you call the function and passing the code as its argument, otherwise, you call the getFreeCode function again and passing the same callback to it. Although you might consider cases when an error happens. If your the db call fails, your callback would never get called. It is better to use a throw/catch mechanism or passing another argument for error to your callback.
You can achieve what you need to do by doing it this way:
function getFreeCode(callback) {
var chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
var code = "";
for (var i = 0; i < 4; i++) {
var rand = Math.floor(Math.random() * chars.length);
console.log(rand);
code += chars.charAt(rand);
}
console.log("Code: %s generated.", code);
client.execute("select * from codes where code="+code, function(err, result) {
if(!err) {
if(result.rows.length > 0) {
getFreeCode(callback);
} else {
callback(code);
}
}else {
console.log('DB ERR: %s', err);
}
console.log(codeFree);
});
console.log('here');
}
// in your main:
getFreeCode(function (code) {
console.log(' this code was free: ' + code)
})
I recommend you look into two alternatives to help deal with asynchronous code.
node generator functions using the 'yield' keyword
promises
Using generators requires running a recent version of node with the --harmony flag. The reason I recommend generators is because you can write code that flows the way you expect.
var x = yield asyncFunction();
console.log('x = ' + x);
The previous code will get the value of x before logging x.
Without yielding the console.log would write out x before the async function was finished getting the value for x.
Your code could look like this with generators:
var client = {
execute: function (query) {
var timesRan = 0;
var result = [];
return function () {
return setTimeout(function () {
result = ++timesRan < 4 ? ['length_will_be_1'] : [];
return result;
},1);
};
}
};
function* checkCode () {
var code;
var codeFree = false;
while(!codeFree) {
var chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
code = "";
for (var i = 0; i < 4; i++) {
var rand = Math.floor(Math.random() * chars.length);
console.log(rand);
code += chars.charAt(rand);
}
console.log("Code: %s generated.", code);
try {
var result = yield client.execute("select * from codes where code="+code);
codeFree = result.rows.length > 0 ? false : true;
}catch(e) {
console.log('DB ERR: %s', err);
} finally {
console.log(codeFree);
}
console.log('here');
}
}
checkCode().next();
You would leave off the client object. I only added that to make a working example that fakes an async call.
If you have to use an older version of node or do not like the yield syntax then promises could be a worthy option.
There are many promise libraries. The reason I recommend promises is that you can write code that flows the way you expect:
asyncGetX()
.then(function (x) {
console.log('x: ' + x);
});
The previous code will get the value of x before logging x.
It also lets you chain async functions and runs them in order:
asyncFunction1()
.then(function (result) {
return asyncFunction2(result)
})
.then(function (x) { /* <-- x is the return value from asyncFunction2 which used the result value of asyncFunction1 */
console.log('x: ' + x);
});
Your code could look like this with the 'q' promise library:
var Q = require('q');
var client = {
timesRan: 0,
execute: function (query, callback) {
var self = this;
var result = {};
setTimeout(function () {
console.log('self.timesRan: ' + self.timesRan);
result.rows = ++self.timesRan < 4 ? ['length = 1'] : [];
callback(null, result);
},1);
}
};
function checkCode () {
var deferred = Q.defer();
var codeFree = false;
var chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
var code = "";
for (var i = 0; i < 4; i++) {
var rand = Math.floor(Math.random() * chars.length);
console.log('rand: %s', rand);
code += chars.charAt(rand);
}
console.log("Code: %s generated.", code);
client.execute("select * from codes where code="+code, function(err, result) {
console.log('err: '+err+', result: ' + JSON.stringify(result));
console.log('result.rows.length: ' + result.rows.length);
if(!err) {
if(result.rows.length > 0) {
codeFree = false;
console.log('result.rows: %s, codeFree: %s', result.rows, codeFree);
checkCode();
} else {
codeFree = true;
console.log('line 36: codeFree: ' + codeFree);
deferred.resolve(code);
}
}else {
console.log('DB ERR: %s', err);
deferred.reject(err);
}
console.log(codeFree);
});
console.log('waiting for promise');
return deferred.promise;
}
checkCode()
.then(function (code) {
console.log('success with code: ' + code);
})
.fail(function(err) {
console.log('failure, err: ' + err);
});
Also omit the client object here. I only added that to make a working example that fakes an async call.
Promises and generators definitely take some time to get used to. It's worth it because they make the code a lot easier to follow in the end than code written with nested callbacks.

Javascript Push array dosen't work

Hi i can't get my push function to work..
my code
var result = {};
dpd.timesheetsdone.get(function (timesheets, err) {
if(err) return console.log(err);
timesheets.forEach(function(entry, index) {
result[entry.timesheets[0].week] = [];
result[entry.timesheets[0].week].push(entry);
//heres my push
}
);
setResult(result);
});
When runing it dosent push my entry, it just does like = and set result to 1 item, it dosen't push.
What could be the problem here ?
example of timesheets
http://pastebin.com/w2i7xYgm
The problem is here:
result[entry.timesheets[0].week] = [];
If you've ever put anything there before, that wipes it out; so you only ever see the last thing for that week.
You only want to do that assignment if you haven't already done it:
if (!result[entry.timesheets[0].week]) {
result[entry.timesheets[0].week] = [];
}
E.g.:
var result = {};
dpd.timesheetsdone.get(function(timesheets, err) {
if (err) return console.log(err);
timesheets.forEach(function(entry, index) {
if (!result[entry.timesheets[0].week]) {
result[entry.timesheets[0].week] = [];
}
result[entry.timesheets[0].week].push(entry);
});
setResult(result);
});
Or to avoid repeated object lookups:
var result = {};
dpd.timesheetsdone.get(function(timesheets, err) {
if (err) return console.log(err);
timesheets.forEach(function(entry, index) {
var week = entry.timesheets[0].week;
var weekArray = result[week];
if (!weekArray) {
weekArray = result[week] = [];
}
weekArray.push(entry);
});
setResult(result);
});

Categories