Reputation: 308
I'm creating a polling app that should log the users ip, and which selection they voted for in addition to what the time was when they voted.
To do this I imagined creating an object for each 'vote' that included each of these as a property and then pushing that object into an array would suit my needs quite well.
Everything was running fine up until I tried to use push my object into my array. What I have currently says that that my array is undefined.
What exactly is happening? I tried using an index and setting votingArray[i] to the object, but that gave an undefined error as well.
Here is my code:
//Vote for the pet passed into by user
function vote(pet, time){
linkService.incrementCount(pet);
trackVote(pet, time);
res.render('vote', {
pet: pet
});
}
var votingArray = [];
function trackVote(pet, time) {
var voteObject = {
time: time,
pet: pet,
ip: req.ip
};
console.log(voteObject);
votingArray.push(voteObject);
console.log(votingArray);
}
function showResults(){
res.render('results', {
votingArray: votingArray
});
}
And here is the error in my console:
(Line 69 is 'votingArray.push(voteObject);')
Express started on http://localhost:8080; press Ctrl-C to terminate.
{ time: 1474584882143, pet: 'scout', ip: '::ffff:10.240.1.15' }
TypeError: Cannot read property 'push' of undefined
at trackVote (/home/ubuntu/workspace/Projects/projectTwo/app.js:69:14)
at vote (/home/ubuntu/workspace/Projects/projectTwo/app.js:51:6)
at /home/ubuntu/workspace/Projects/projectTwo/app.js:43:6
at Layer.handle [as handle_request] (/home/ubuntu/workspace/Projects/projectTwo/node_modules/express/lib/router/layer.js:95:5)
at next (/home/ubuntu/workspace/Projects/projectTwo/node_modules/express/lib/router/route.js:131:13)
at Route.dispatch (/home/ubuntu/workspace/Projects/projectTwo/node_modules/express/lib/router/route.js:112:3)
at Layer.handle [as handle_request] (/home/ubuntu/workspace/Projects/projectTwo/node_modules/express/lib/router/layer.js:95:5)
at /home/ubuntu/workspace/Projects/projectTwo/node_modules/express/lib/router/index.js:277:22
at param (/home/ubuntu/workspace/Projects/projectTwo/node_modules/express/lib/router/index.js:349:14)
at param (/home/ubuntu/workspace/Projects/projectTwo/node_modules/express/lib/router/index.js:365:14)
Edit:
Due to a lot of people suggesting that I've declared, but not instantiated the varriable 'votingArray' as an array, and that my snippets haven't included everything I've rewritten what I have to meet those comments and answers.
I'm still getting the same error about votingArray.push.
Here is my new code:
var votingArray = [];
//Vote for the pet passed into by user
function vote(pet, time){
linkService.incrementCount(pet);
var votingObject =
{
ip: req.ip,
pet: pet,
timecode: time
};
votingArray.push(votingObject);
res.render('vote', {
pet: pet
});
}
function showResults(){
res.render('results', {
votingArray: votingArray
});
}
Upvotes: 1
Views: 24922
Reputation: 2624
initialize your array or pass it into the function instead of having a floating var in your file. We also can't see the usage of where trackVote is called in your question, but this would be my suggestion.
var votingArray = [];
function trackVote(pet, time) {
var voteObject = {
time: time,
pet: pet,
ip: req.ip
};
console.log(voteObject);
votingArray.push(voteObject);
console.log(votingArray);
}
to
function trackVote(pet, time) {
var votingArray = [];
var voteObject = {
time: time,
pet: pet,
ip: req.ip
};
console.log(voteObject);
votingArray.push(voteObject);
console.log(votingArray);
}
or
function trackVote(pet, time, votingArray) {
var voteObject = {
time: time,
pet: pet,
ip: req.ip
};
console.log(voteObject);
votingArray.push(voteObject);
console.log(votingArray);
}
Upvotes: 0
Reputation:
Your variable votingArray
is not initialized. If you want it to be an array, you must replace the line
var votingArray;
with
var votingArray = []; // Empty array instance
Upvotes: 2