下面,您可以看到这两个日志的输出。第一行代码清楚地显示了我试图访问的属性的完整对象,但在下一行代码中,我无法使用配置访问它。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);
这就是这些行在控制台中打印的内容
当前回答
我今天也遇到了同样的问题。问题是由uglify-js引起的。在我执行了相同的非丑陋代码后,问题得到了解决。删除的
--mangle-props
从uglify-js中编写丑陋的代码就足够了。
也许,最好的做法是为那些必须被uglify-js的regex规则破坏的属性使用一些前缀。
来源如下:
var data = JSON.parse( content);
...
this.pageIndex = parseInt(data.index);
this.pageTotal = parseInt(data.total);
this.pageLimit = parseInt(data.limit);
它是这样被丑化的:
var n = JSON.parse( t);
...
this._ = parseInt(n.index), this.g = parseInt(n.total), this.D = parseInt(n.C)
其他回答
我今天也遇到了同样的问题。在我的例子中,键是嵌套的,即key1.key2。 我使用split()分割键,然后使用方括号符号,这对我来说很有效。
var data = {
key1: {
key2: "some value"
}
}
我把键分开并像这样使用它,data[key1][key2]为我做了这项工作。
关于这个问题,我已经尝试了很多答案 终于发现发生了什么事。
虽然对象在console.log(foo)上打印,但只有在使用new foo()时才会生成get和set方法。 你必须用平面js对象初始化实例
请看:https://stackoverflow.com/a/41153231/5954248
我没有得到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\" }"
}
}
*/
我也遇到了同样的问题,但上面的解决方案对我来说都不奏效,之后我感觉就像是在猜测。但是,在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
});
在我的情况下,它只是碰巧是,即使我接收到的数据在一个模型的格式,如myMethod(数据:MyModelClass)对象直到接收到的对象是类型字符串。 也就是console.log(data)中的y,我得到了内容。 解决方案就是解析JSON(在我的例子中)
const model:MyMOdelClass=JSON.parse(data);
思想可能是有用的。