TypeError:无法读取属性' name'在节点js代码中未定义

时间:2016-07-15 08:00:32

标签: javascript node.js

如果条件columnName ==' name'

,我收到错误
receivedValues = req.body
var usercolumns = ["name","price","duration","gender"];

    //FOR VALIDATING VALUES BEFORE SUBMISSION
    for(var iter=0;iter<usercolumns.length;iter++)

    {
        columnName = usercolumns[iter];
        console.log("column name",columnName);
        if(receivedValues[columnName] == undefined && (columnName=='name' || columnName=='price' || columnName=='duration' || columnName=='gender'))
        {

enter image description here

2 个答案:

答案 0 :(得分:2)

此处receivedValues变量似乎未定义,您应该添加更多代码进行提问。

答案 1 :(得分:0)

if(receivedValues && receivedValues[columnName] == undefined && (columnName=='name' || columnName=='price' || columnName=='duration' || columnName=='gender'))

您需要检查 receivedValues 是否存在

您可以优化代码:

if(receivedValues && !columnName in receivedValues && columnName in usercolumns)