下面,您可以看到这两个日志的输出。第一行代码清楚地显示了我试图访问的属性的完整对象,但在下一行代码中,我无法使用配置访问它。Col_id_3(见截图中的“undefined”?)有人能解释一下吗?我也可以访问除field_id_4之外的所有其他属性。

console.log(config);
console.log(config.col_id_3);

这就是这些行在控制台中打印的内容


当前回答

我也有类似的问题,或者只是相关的问题。

对于我的情况,我正在访问一个对象的属性,但其中一个是未定义的。我发现问题是在服务器端代码的空白,而创建的关键,对象的val。

我的方法是这样的……

在从创建对象的服务器端代码中删除空白之后,我现在可以像下面这样访问属性了…

这可能不是主题问题的问题,但对我来说是这样,对其他人来说可能也是这样。希望能有所帮助。

其他回答

我也遇到了同样的问题,但上面的解决方案对我来说都不奏效,之后我感觉就像是在猜测。但是,在setTimeout函数中包装创建对象的代码对我来说是有用的。

setTimeout(function() {
   var myObj = xyz; //some code for creation of complex object like above
   console.log(myObj); // this works
   console.log(myObj.propertyName); // this works too
});

我也有类似的问题,或者只是相关的问题。

对于我的情况,我正在访问一个对象的属性,但其中一个是未定义的。我发现问题是在服务器端代码的空白,而创建的关键,对象的val。

我的方法是这样的……

在从创建对象的服务器端代码中删除空白之后,我现在可以像下面这样访问属性了…

这可能不是主题问题的问题,但对我来说是这样,对其他人来说可能也是这样。希望能有所帮助。

我刚刚遇到了由CSV -parser从MS Excel生成的CSV文件生成的对象的问题。我能够访问除第一个属性之外的所有属性-但如果我使用console.log编写整个对象,它将显示ok。

结果UTF-8 CSV格式在开头插入3个字节(ef bb bf),对应于一个不可见的字符-这被包括在CSV -parser的第一个属性头中。解决方案是使用非utf选项重新生成CSV,这消除了不可见字符。

没有一个JSON stringify/parse对我有用。

formValues.myKey:               undefined
formValues.myKey with timeout:  content

我想要formValues的值。myKey是什么技巧是setTimeout 0,就像下面的例子。希望能有所帮助。

console.log('formValues.myKey: ',formValues.myKey);
setTimeout( () => { 
  console.log('formValues.myKey with timeout: ', formValues.myKey);
}, 0 );

我没有得到MongoDB错误消息在抛出错误在我的NodeJS API响应,所以我做了以下工作

// It was not working
console.log(error.message) // prints the error
let response = error;
// message property was not available in the response.
/* 
{
  "status": "error",
  "error": {
    "driver": true,
    "name": "MongoError",
    "index": 0,
    "code": 11000,
    "keyPattern": {
      "event_name": 1
    },
    "keyValue": {
      "event_name": "followup"
    }
  }
}
*/
// so I did this
let message = error.message;
let response = JSON.parse(JSON.stringify(error));
response.message = message;
// message property is now available in the response.
/* 
{
  "status": "error",
  "error": {
    "driver": true,
    "name": "MongoError",
    "index": 0,
    "code": 11000,
    "keyPattern": {
      "event_name": 1
    },
    "keyValue": {
      "event_name": "followup"
    },
    "message": "E11000 duplicate key error collection: mycollections.notificationevents index: event_name_1 dup key: { event_name: \"followup\" }"
  }
}
*/