下面,您可以看到这两个日志的输出。第一行代码清楚地显示了我试图访问的属性的完整对象,但在下一行代码中,我无法使用配置访问它。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,这消除了不可见字符。

其他回答

我刚刚从MongoDB使用Mongoose加载文档时遇到了同样的问题。

原来,我使用属性find()只返回一个对象,所以我把find()改为findOne(),一切都为我工作。

解决方案(如果您正在使用Mongoose):确保只返回一个对象,这样您就可以解析它的对象。Id或者它将被视为数组所以你需要像访问对象[0]。Id那样访问它。

我也遇到过类似的问题(在为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 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使用Mongoose加载文档时遇到了这个问题。

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

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

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

这可能会帮助某人,因为我有一个类似的问题,其中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...