如何使用节点js转换属性/变量

时间:2017-12-21 07:38:16

标签: node.js chatfuel

我用聊天燃料创建了一个机器人。并且它询问机器人让你选择的性别是男性还是女性......两者都被设置为一个属性gender但我想要做的是改变每个选择的值,就像我选择男性的那样如果是女性0 ..

this is what it looks like...

当它保存到我的数据库gender = malegender = female时,我想要实现的是当我选择男性时,将保存的值为1,如果女性为0,那么它看起来像我的数据库gender = 1如果malegender = 0 female

router.post('/chatbot_patient', (req, res) => {
    var ref = database.ref('patients');
    var body = _.pick(_.assign(patient, req.body), _.keys(patient));


    body['client_fk'] = req.body['chatfuel user id'];

    req.body['1'] = req.body['male'];
    req.body['0'] = req.body['female'];
    body['gender'] = req.body['gender1'];
    body['createdAt'] = moment().format('YYYY-MM-DD HH:mm:ss');
    ref.push(body);

    res.json({
                 "messages": [
                   {"text": "You are now registered as iWatcher."}
                 ]
                });
});

这是我的节点脚本看起来的样子。我试过转换它但它不起作用..有没有办法在节点上做到这一点?

1 个答案:

答案 0 :(得分:0)

尝试取值并将其设置在另一个变量中:

var gender = req.body['gender'] === 'male' ? 1: 0;