admin管理员组文章数量:1398831
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 ments 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
});
}
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 ments 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
});
}
Share
Improve this question
edited Sep 22, 2016 at 23:26
Augie Luebbers
asked Sep 22, 2016 at 23:01
Augie LuebbersAugie Luebbers
3081 gold badge2 silver badges15 bronze badges
9
-
3
votingArray
isundefined
. Exactly what the error says. – VLAZ Commented Sep 22, 2016 at 23:02 - Yes, but why is it undefined? I've declared it, and defining if I don't push into the array and console.log it, I receive the same error. – Augie Luebbers Commented Sep 22, 2016 at 23:03
-
2
try to initialize
votingArray
with initial value, likevar votingArray = [];
– Telman Commented Sep 22, 2016 at 23:04 -
You have declared it. And that's it. It does not magically bee an array when you call
.push
on anundefined
thing. – VLAZ Commented Sep 22, 2016 at 23:04 - 1 @AugieLuebbers. Please, do not edit your question like you did, because it disrupts the relation between your original question and my answer. Instead, place your edit as a new paragraph. If you still have the same error, you need to provide us more informations, because this is not enough to understand how your whole code works. – user6586783 Commented Sep 22, 2016 at 23:12
2 Answers
Reset to default 2Your 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
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);
}
本文标签: javascriptUsing arraypush yields undefinedStack Overflow
版权声明:本文标题:javascript - Using array.push yields undefined - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744123217a2591837.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论