简体   繁体   English

使用array.push产生未定义

[英]Using array.push yields undefined

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. 我正在创建一个轮询应用程序,该应用程序应该记录用户的IP,以及他们投票的时间以及他们投票的选择。

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. 我尝试使用索引并为该对象设置votingArray [i],但这也产生了未定义的错误。

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);') (第69行是'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. 由于很多人建议我已声明但未实例化可变的“ votingArray”作为数组,并且我的代码片段并未包含我重写遇到的所有评论和答案所需的内容。

I'm still getting the same error about votingArray.push. 关于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
    });
}

Your variable votingArray is not initialized. 您的变量votingArray未初始化。 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. 初始化数组或将其传递到函数中,而不是在文件中包含浮动var。 We also can't see the usage of where trackVote is called in your question, but this would be my suggestion. 我们也看不到在您的问题中调用trackVote的用法,但这是我的建议。

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

}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM