首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >有人搜索空对象时的响应

有人搜索空对象时的响应
EN

Stack Overflow用户
提问于 2019-07-25 21:11:12
回答 1查看 24关注 0票数 0

我正在尝试从用于SAP对话式AI的sql获取响应。在下面的代码中,一切正常,但是如果有一个空对象,那么代码就会停止。如果有空对象,我需要响应。

代码语言:javascript
复制
app.post('/packers', (req, res) => {
console.log(req.body.nlp.source)
rand = random.number(1, 9)

let hp = req.body.nlp.source

let sql= `SELECT * FROM data where catname = 9 AND address LIKE '%${hp}%' `;
let query = db.query(sql, (err, result)=> {
if (err) throw err;
Shopname = result[rand].shopname;
Adress = result[rand].address;
Mobile = result[rand].mobile;
res.send({
replies: [{ type: 'text',
 content: `Name of shop is ${Shopname} and address is ${Adress} and mobile no is ${Mobile}`,
}],
conversation: {
memory: { key: 'value' }
}
})
})
})
EN

回答 1

Stack Overflow用户

发布于 2019-07-25 21:24:45

你可以使用像Lodash get()这样的东西

代码语言:javascript
复制
// this can throw if nlp isn't defined 
let hp = req.body.nlp.source

// this doesn't throw
let hp = _.get(req, 'body.nlp.source', null)

// For this case it throws if result[rand] isn't defined
Shopname = result[rand].shopname

// you can solve like this
Shopname = result[rand] ? result[rand].shopname : null
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/57202770

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档