下面,您可以看到这两个日志的输出。第一行代码清楚地显示了我试图访问的属性的完整对象,但在下一行代码中,我无法使用配置访问它。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);
这就是这些行在控制台中打印的内容
当前回答
在我的情况下,它只是碰巧是,即使我接收到的数据在一个模型的格式,如myMethod(数据:MyModelClass)对象直到接收到的对象是类型字符串。 也就是console.log(data)中的y,我得到了内容。 解决方案就是解析JSON(在我的例子中)
const model:MyMOdelClass=JSON.parse(data);
思想可能是有用的。
其他回答
在我的情况下,我将一个对象传递给一个承诺,在承诺中,我向对象添加了更多的键/值,当它完成时,承诺返回对象。
然而,我稍微多看了一下,承诺是在完全完成之前归还对象……因此,我的其余代码试图处理更新的对象,而数据还没有出现。但就像上面一样,在控制台中,我看到对象完全更新,但无法访问键-它们返回时未定义。直到我看到了这个:
console.log(obj) ;
console.log(obj.newKey1) ;
// returned in console
> Object { origKey1: "blah", origKey2: "blah blah"} [i]
origKey1: "blah"
origKey2: "blah blah"
newKey1: "this info"
newKey2: "that info"
newKey3: " more info"
> *undefined*
[i]是一个小图标,当我悬停在它上面时,它说左边的对象值在记录时被快照,下面的值刚刚被评估。这时我突然想到,我的对象在承诺完全更新之前就已经被评估了。
我也有类似的问题,希望下面的解决方案能帮助到别人。 你可以使用setTimeout函数,但你永远不知道你的浏览器需要多长时间来定义你的对象。
除此之外,我建议使用setInterval函数。它将等待您的对象配置。Col_id_3被定义,然后触发需要特定对象属性的下一个代码部分。
window.addEventListener('load', function(){
var fileInterval = setInterval(function() {
if (typeof config.col_id_3 !== 'undefined') {
// do your stuff here
clearInterval(fileInterval); // clear interval
}
}, 100); // check every 100ms
});
我也遇到过类似的问题(在为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);
}
});
检查是否在控制台中应用了任何筛选器。它发生在我在chrome控制台。
您试图访问的属性可能还不存在。Console.log可以工作,因为它在一小段延迟后执行,但其余代码并非如此。试试这个:
var a = config.col_id_3; //undefined
setTimeout(function()
{
var a = config.col_id_3; //voila!
}, 100);