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

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

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


当前回答

2018年,Mozilla在Mozilla文档中警告我们!

我引用“日志对象”:

不要使用console.log(obj); 使用console.log (JSON.parse (JSON.stringify (obj)));。 通过这种方式,您可以确定当前正在看到obj的值 你记录下来。

其他回答

如果你使用的是TYPESCRIPT和/或ANGULAR,它可能是这样的!

.then((res: any) => res.json())

设置响应类型为任何为我修复了这个问题,我无法访问响应上的属性,直到我设置res: any

属性“_body”在类型“Response”上不存在

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

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

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

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

我也遇到过类似的问题(在为SugarCRM开发游戏时),我的出发点是:

var leadBean = app.data.createBean('Leads', {id: this.model.attributes.parent_id});

// This should load object with attributes 
leadBean.fetch();

// Here were my attributes filled in with proper values including name
console.log(leadBean);

// Printed "undefined"
console.log(leadBean.attributes.name);

问题是在fetch(),它的异步调用,所以我必须重写我的代码:

var leadBean = app.data.createBean('Leads', {id: this.model.attributes.parent_id});

// This should load object with attributes 
leadBean.fetch({
    success: function (lead) {
        // Printed my value correctly
        console.log(lead.attributes.name);
    }
});

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

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

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

首先,像下面这样检查类型:

console.log(typeof config);

如果上面的命令打印object,那么很简单,你只需使用括号符号。如果您想动态搜索属性的值,括号符号可能非常有用。

执行以下命令:

console.log(config[col_id_3]);

如果它是一个字符串,你需要先在对象中解析。要做到这一点,你需要执行以下命令:

var object = JSON.parse(config);

然后使用括号来访问属性,如下所示:

console.log(object[col_id_3]);