1

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
    });
}
9
  • 3
    votingArray is undefined. Exactly what the error says. 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. Commented Sep 22, 2016 at 23:03
  • 2
    try to initialize votingArray with initial value, like var votingArray = []; Commented Sep 22, 2016 at 23:04
  • You have declared it. And that's it. It does not magically become an array when you call .push on an undefined thing. 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. Commented Sep 22, 2016 at 23:12

2 Answers 2

2

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
Sign up to request clarification or add additional context in comments.

4 Comments

To make it behave like an array well, it will be an array.
Declaring it as such yields the same error code. I've updated my question.
Have you restarted the app after that?
I have restarted the app and have revised my question once more.
0

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);

}

3 Comments

I've trimmed down my code quite a bit, but before that even following your method gave me the same error.
Where are you calling trackVote?
With my current code, I no longer need to. I just do what the function trackVote did in function vote.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.