使用array.push会产生未定义的

时间:2016-09-22 23:01:01

标签: javascript arrays json node.js

我正在创建一个应该记录用户IP的轮询应用,以及他们投票的选择以及他们投票的时间。

要做到这一点,我想象为每个'投票创造一个对象。其中包含每个属性作为属性然后将该对象推入数组将很好地满足我的需求。

一切都运行良好,直到我尝试将我的对象推入我的数组。我目前所说的我的数组是未定义的。

到底发生了什么?我尝试使用索引并将votingArray [i]设置为该对象,但这也给出了一个未定义的错误。

这是我的代码:

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

这是我的控制台中的错误:

(第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)

编辑:

由于很多人建议我已经宣布,但没有实例化可变的“投票”和“投票”。作为一个数组,我的片段中没有包含我已经重写的所有内容,以满足这些评论和答案。

我仍然得到与votingArray.push相同的错误。

这是我的新代码:

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

2 个答案:

答案 0 :(得分:2)

您的变量 on sheet 2 cell A1 should have cat + blue + active and on sheet 2 cell A2 should have cat + orange + active Sheet1 row row column cat blue active dog orange slow green Sheet2 COLUMN1 COLUMN2 ROW 1 catblueactive catblueslow ROW 2 catorangeactive catorangeslow ROW 3 catgreenactive catgreenslow ROW 4 dogblueactive dogblueslow ROW 5 dogorangeactive dogorangeslow ROW 6 doggreenactive doggreenslow 未初始化。如果您希望它是一个数组,则必须替换

votingArray

var votingArray;

答案 1 :(得分:0)

初始化数组或将其传递给函数,而不是在文件中包含浮动var。我们也无法看到问题中调用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);

}

function trackVote(pet, time) {
    var votingArray = [];
    var voteObject = {
        time: time,
        pet: pet,
        ip: req.ip
        };

    console.log(voteObject);
    votingArray.push(voteObject);
    console.log(votingArray);

}

function trackVote(pet, time, votingArray) {

    var voteObject = {
        time: time,
        pet: pet,
        ip: req.ip
        };

    console.log(voteObject);
    votingArray.push(voteObject);
    console.log(votingArray);

}