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

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

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


当前回答

这可能会帮助某人,因为我有一个类似的问题,其中JSON.parse()返回一个对象,我可以在console.log()上打印,但我无法访问特定的字段,上述解决方案都不适合我。比如使用JSON.parse()和JSON.stringify()的组合。

var jsonObj = JSON.parse(JSON.stringify(responseText))

// where responseText is a JSON String returned by the server.

console.log(jsonObj) ///Was printing the object correctly
console.log(jsonObj.Body) /// Was printing Undefined  

我最终通过使用ExtJs Ext.decode()提供的不同解析器来解决这个问题;

var jsonObj = Ext.decode(responseText)
console.log(jsonObj.Body) //Worked...

其他回答

console.log(anObject)的输出是误导性的;通过单击>展开控制台中显示的对象树,显示的对象的状态才会解析。当你console.log对象时,它不是对象的状态。

相反,尝试console.log(object .keys(config)),甚至console.log(JSON.stringify(config)),您将看到调用console.log时的键或对象的状态。

您将(通常)发现在console.log调用之后添加了键。

检查对象内部是否有一个对象数组。我有一个类似的问题与JSON:

    "terms": {
        "category": [
            {
                "ID": 4,
                "name": "Cirugia",
                "slug": "cirugia",
                "description": "",
                "taxonomy": "category",
                "parent": null,
                "count": 68,
                "link": "http://distritocuatro.mx/enarm/category/cirugia/"
            }
        ]
    }

我试图从“类别”访问“名称”键,我得到了未定义的错误,因为我正在使用:

var_name = obj_array.terms.category.name

然后我意识到它有方括号,这意味着它在category键中有一个对象数组,因为它可以有多个category对象。因此,为了获得'name'键,我使用了这个:

var_name = obj_array.terms.category[0].name

这就成功了。

也许现在回答这个问题已经太晚了,但我希望有同样问题的人能像我一样在找到解决方案之前找到这个答案:)

以防这对某人有帮助,我有一个类似的问题,这是因为有人在我正在使用的对象中创建了. tojson的覆盖。所以对象是这样的:

{
  foo: {
         bar: "Hello"
         baz: "World"
       }
}

但是.toJSON()是:

toJSON() {
  return this.foo
}

所以当我调用JSON.stringify(myObject)它返回"{"bar": "Hello", "baz": "World"}"。然而,Object.keys(myObject)显示了“foo”。

我刚刚从MongoDB使用Mongoose加载文档时遇到了这个问题。

当在整个对象上运行console.log()时,所有文档字段(存储在db中)都会显示出来。然而,当其他属性(包括_id)正常工作时,一些单独的属性访问器将返回undefined。

事实证明,属性访问器只适用于我的mongodb . schema(…)定义中指定的字段,而console.log()和JSON.stringify()返回存储在db中的所有字段。

解决方案(如果你正在使用Mongoose):确保你所有的db字段都是在Mongoose . schema(…)中定义的。

我没有得到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\" }"
  }
}
*/