Javascript 如果条件运行即使为false,也很混乱
我有下面的代码,在代码块的末尾有一个if语句,但即使它是false,after代码仍然运行,数据被添加到MongoDB中。我不明白为什么 当我通过发送get请求来测试它时,这些项会被添加到MongoDB中Javascript 如果条件运行即使为false,也很混乱,javascript,node.js,mongodb,express,axios,Javascript,Node.js,Mongodb,Express,Axios,我有下面的代码,在代码块的末尾有一个if语句,但即使它是false,after代码仍然运行,数据被添加到MongoDB中。我不明白为什么 当我通过发送get请求来测试它时,这些项会被添加到MongoDB中 const router = require('express').Router(); const xml2js = require('xml2js'); const Avaya = require('../../models/avayaLogs') router.route('/').ge
const router = require('express').Router();
const xml2js = require('xml2js');
const Avaya = require('../../models/avayaLogs')
router.route('/').get(async (req, res) => {
const axios = require("axios");
const getData = axios( {
url: "http://127.0.0.1:60000/onexagent/api/nextnotification?clientid=65e814f0-e7d8-4404-b9e5-203bfc7935c5",
})
const data = await getData;
const converted = xml2js.parseStringPromise(data.data, { mergeAttrs: true })
.then(result => result);
const logs = await converted;
const notifType = []
const notifDetails = []
for (let i in logs.NextNotificationResponse) {
notifType.push(i)
}
for (let i in logs.NextNotificationResponse[notifType[1]][0])
{notifDetails.push(i)}
const arranged = {
NotificationType: notifType[1],
ResponseCode: logs.NextNotificationResponse[notifType[0]][0]
}
for (let i = 0; i < notifDetails.length; i++)
{
arranged[[notifDetails[i]][0]]= logs.NextNotificationResponse[notifType[1]][0][notifDetails[i]][0]
}
console.log(arranged)
if (arranged.NotificationType === 'VoiceInteractionCreated' || 'VoiceInteractionMissed' || 'VoiceInteractionTerminated') {
const newLogs = new Avaya({
notification: arranged
});
newLogs.save()
.then(logs => res.json(logs))
.catch(err => console.error(err));
}
});
module.exports = router;
您应该分别比较它们:
if (arranged.NotificationType === 'VoiceInteractionCreated' || arranged.NotificationType === 'VoiceInteractionMissed' || arranged.NotificationType === 'VoiceInteractionTerminated')
您应该分别比较它们:
if (arranged.NotificationType === 'VoiceInteractionCreated' || arranged.NotificationType === 'VoiceInteractionMissed' || arranged.NotificationType === 'VoiceInteractionTerminated')
在上面的代码中,我添加了一些括号,告诉您为什么它会返回true。Javascript中考虑了字符串
这将为您提供所需的输出
if (arranged.NotificationType === 'VoiceInteractionCreated' || arranged.NotificationType === 'VoiceInteractionMissed' || arranged.NotificationType === 'VoiceInteractionTerminated') {
在上面的代码中,我添加了一些括号,告诉您为什么它会返回true。Javascript中考虑了字符串
这将为您提供所需的输出
if (arranged.NotificationType === 'VoiceInteractionCreated' || arranged.NotificationType === 'VoiceInteractionMissed' || arranged.NotificationType === 'VoiceInteractionTerminated') {
arranged.NotificationType===“VoiceInteractionCreated”| |“VoiceInteractionMissed”| | |“VoiceInteractionTerminated”不是检查多个值的有效方法。“Foo”| |“Bar”始终为真。arranged.NotificationType===“VoiceInteractionCreated”| |“VoiceInteractionMissed”| |“VoiceInteractionTerminated”不是检查多个值的有效方法。“Foo”| |“Bar”永远是真的。