下面,您可以看到这两个日志的输出。第一行代码清楚地显示了我试图访问的属性的完整对象,但在下一行代码中,我无法使用配置访问它。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);
这就是这些行在控制台中打印的内容
当前回答
我今天在这个问题上很纠结,我想我会留下我的解决方案。
我通过ajax获取一个数据对象,就像这样: {“常量”:{“value1”:“x”,“value2”:“y”},“国际化”{“data1”:“x”,“data2 ": " y "}}
假设这个对象在一个名为data的变量中。每当我引用数据时。i18n没有定义。
Console.log (data)显示了预期的对象 console.log(Object.keys(data))显示["constants","i18n"] 将i18n重命名为inter并没有改变任何东西 我甚至尝试切换数据,使“i18n”成为第一个对象 移动代码以确保完全设置了对象,ajax承诺也没有问题。
没有什么帮助…然后在服务器端,我将数据写入php日志,它揭示了这一点:
{“常量”:{“value1”:“x”,“value2”:“y”},“\ u045618n”{“data1”:“x”,“data2 ": " y "}}
索引键中的“i”实际上是一个u0456(西里尔字母i)。这在我的php编辑器或浏览器控制台日志中不可见。只有php日志显示这个…这是个棘手的问题……
其他回答
我没有得到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\" }"
}
}
*/
对我来说,这是一个与猫鼬有关的问题。
我正在循环从Mongo查询中得到的对象。我只需要删除:
items = await Model.find()
并将其替换为:
items = await Model.find().lean()
我今天在React中遇到了类似的问题。最终意识到问题是由尚未确定的状态引起的。我正在调用user.user.name,尽管它显示在控制台中,但我似乎无法在我的组件中访问它,直到我包含了一个检查来检查是否user.name。设置用户,然后调用User . User .name。
如果你使用的是TYPESCRIPT和/或ANGULAR,它可能是这样的!
.then((res: any) => res.json())
设置响应类型为任何为我修复了这个问题,我无法访问响应上的属性,直到我设置res: any
属性“_body”在类型“Response”上不存在
console.log(anObject)的输出是误导性的;通过单击>展开控制台中显示的对象树,显示的对象的状态才会解析。当你console.log对象时,它不是对象的状态。
相反,尝试console.log(object .keys(config)),甚至console.log(JSON.stringify(config)),您将看到调用console.log时的键或对象的状态。
您将(通常)发现在console.log调用之后添加了键。