mysql - 如何从 findOne 函数 MySQL 返回布尔值
问题描述
我想将结果findOne
用作if
语句的条件。我无法返回,true
或者false
,我看到它的async
功能有所不同,但尽管多次尝试都没有奏效。
控制器.js
// Find a single User with a sub
exports.findOne = (req, res) => {
const sub = req.params.sub;
User.findOne({sub})
.then(data => {
res.send(data);
})
.catch(err => {
res.status(500).send({
message: "Error retrieving User with sub=" +sub
});
});
};
前端:
//INFO SAVE AND UPDATE CONDITION
async function exist() {
let x = await InfoDataService.get(data.sub)
console.log(x);
if (x === null) {
return true;
}
return false;
}
if ( exist() ) {
InfoDataService.create(data)
.then((response) => {
console.log('create', response.data);
setInfo({
id: response.data.id,
sub: response.data.sub,
email: response.data.email,
firstname: response.data.firstname,
lastname: response.data.lastname
});
})
} else {
InfoDataService.update(sub, data)
.then((response) => {
console.log(response.data);
})
.catch((e) => {
console.error(e);
});
}
};
解决方案
您有 2 个选项可以做到这一点。两者只是相互补充。
// option 1
if (x) {
return true;
}
return false;
// OR
// option 2
if (!x) {
return false;
}
return true;
推荐阅读
- xcode - 将目标添加到 iOS/macOS 多平台项目 (Xcode 12)
- c# - 如何使用 C# 在 mongodb 中插入许多() JSON 数组
- python - 模型在 Keras 中运行良好,但在 Tensorflow 中不行
- bash - 如何根据 shell 脚本中的 for 循环索引增加选项卡数
- r - R:将两列组合转换为各种列
- c++ - Qt 使用 UI 表单创建自定义小部件
- django - 如何在 django 和 DRF 中使用“路由器”
- javascript - 使用本机文件系统 API 将文件保存到特定位置而无需用户交互?
- java - 在 Eclipse 中创建 JAR 失败
- python - 如何在pandas groupby中获得最少的值出现次数