聊天机器人,节点js,访问json数据

时间:2018-01-17 05:15:52

标签: node.js bots

我正在使用节点js和ms bot框架创建一个聊天机器人,我想从data.js访问数据但是我得到了未定义的结果我无法访问示例“west”的日期,代码的任何问题? chabot result screenshot data.js

let salesData  = {
  west: [
    { units: "200", total: "100 pesos" },
   { title: "In The End", artist: "Linkin Park" }

  ],
  func: function() {

  }
};

module.exports = salesData ;

app.js

let salesData  = require('./Dialogs/data.js');
salesData .func();
bot.dialog('getSalesData', [
    function (session) {
        builder.Prompts.choice(session, "Which region would you like sales for?", salesData); 
    },
    function (session, results) {
        if (results.response) {
            var region = salesData[results.response.entity];
            session.send(`We sold ${region.units} units for a total of ${region.total}.`); 
        } else {
            session.send("OK");
        }
    }
]);

1 个答案:

答案 0 :(得分:1)

您的salesData.west是一个数组。

获取单位时,您需要region[0].units

但是,绑定索引并不是一个好主意,因此如果可能,您可能希望重新构建salesData.west

根据您当前的app.js结构,这应该是 data.js

let salesData = {
    west: {
        units: "200",
        total: "100 pesos",
        title: "In The End",
        artist: "Linkin Park"
    },
    func: function() {

    }
};