无法使用'-'Nodejs引用专有名称

时间:2018-08-14 09:33:22

标签: javascript json node.js object properties

这是我使用console.log console.log(category)的对象时的输出。

{ _id: 5b723aa57bab2a0b9a0d1429,
updatedBy: 5b62743ac154c5669e9259d0,
updatedAt: 2018-08-14T09:25:20.050Z,
createdBy: 5b62743ac154c5669e9259d0,
createdAt: 2018-08-14T02:12:53.705Z,
name: 'Community Engagement',
__v: 0,
'dialogflow-agent': '8df9fab841554e8badfe2f66c74e08f3' }

但是奇怪的是,当我console.log(category["dialogflow-agent"])给我undefined

有人知道如何访问属性dialogflow-agent吗?

更新:

JSON.stringify(category)的输出是:

{"_id":"5b6ac11920bea9751e2847e3","updatedBy":"5b62743ac154c5669e9259d0","updatedAt":"2018-08-14T09:03:54.638Z","createdBy":"5b62743ac154c5669e9259d0","createdAt":"2018-08-08T10:08:25.917Z","name":"Awards and Scholarships","__v":0,"dialogflow-agent":"26d927f3f627496fbacc08ac16af898f"}

1 个答案:

答案 0 :(得分:1)

问题中缺少的是Category是猫鼬模式,该模式中的属性缺少dialogflow-agent

let categorySchema = new mongoose.Schema({
    name: String,
    'dialogflow-agent': String,    // This line was missing.
});

感谢@Shilly和@ T.J。Crowder的帮助,对于问题中缺少的信息表示抱歉。但是对于面临此问题的人,请检查您的架构。