我在启动另一个node.js模块时,会传递一个配置文件。该配置文件包含以下JSON:
"resolution": {
"activated": true,
"types": [
{"of": 23}
]
}
当我在被调用的node.js模块中打印接收到的类型数组时,它看起来像这样:
console.log('types array: '+ this.config.resolution.types)
//output
types array: [object Object]
如果我尝试使用
JSON.stringify()
打印文本,则会得到以下结果。[{"of":23}]
当我尝试用从MongoDB数据库中检索到的列表替换types数组时,问题就出现了。我的代码如下:
"resolution": { "activated": true, "types": [ savedTypes ] }
现在,当我打印接收到的types配置时,它看起来像这样:
types array: { _id: 5ab9fe8fd1f64303cd98f122, of: 23, __v: 0 }
我调用的节点模块在这个配置下无法正常工作。如何将其转换为对象?我尝试使用
JSON.parse(savedTypes)
并且出现了错误
SyntaxError: Unexpected token _ in JSON at position 2