下面,您可以看到这两个日志的输出。第一行代码清楚地显示了我试图访问的属性的完整对象,但在下一行代码中,我无法使用配置访问它。Col_id_3(见截图中的“undefined”?)有人能解释一下吗?我也可以访问除field_id_4之外的所有其他属性。
console.log(config);
console.log(config.col_id_3);
这就是这些行在控制台中打印的内容
下面,您可以看到这两个日志的输出。第一行代码清楚地显示了我试图访问的属性的完整对象,但在下一行代码中,我无法使用配置访问它。Col_id_3(见截图中的“undefined”?)有人能解释一下吗?我也可以访问除field_id_4之外的所有其他属性。
console.log(config);
console.log(config.col_id_3);
这就是这些行在控制台中打印的内容
当前回答
我刚刚遇到了由CSV -parser从MS Excel生成的CSV文件生成的对象的问题。我能够访问除第一个属性之外的所有属性-但如果我使用console.log编写整个对象,它将显示ok。
结果UTF-8 CSV格式在开头插入3个字节(ef bb bf),对应于一个不可见的字符-这被包括在CSV -parser的第一个属性头中。解决方案是使用非utf选项重新生成CSV,这消除了不可见字符。
其他回答
我也遇到了同样的问题,但上面的解决方案对我来说都不奏效,之后我感觉就像是在猜测。但是,在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
});
我也遇到过类似的问题(在为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);
}
});
我也有同样的问题。我的解决方案是使用字符串化输出作为解析JSON的输入。这对我很管用。希望对你有用
var x =JSON.parse(JSON.stringify(obj));
console.log(x.property_actually_now_defined);
我也面临同样的问题。
从服务器,我在json_encode中返回数据。 像这样
echo json_encode($res);
当我返回一个不带json_encode的简单PHP数组后,问题就解决了。
return $res;
如果这是在使用Mongoose时发生的问题,则可能会发生以下情况:
console.log(object)
返回所有内容,包括所需的键。
console.log(object.key)
返回未定义。
如果发生了这种情况,就意味着Mongoose Schema中缺少键。添加它将解决这个问题。