我想使用 findOne
的结果作为 if
语句的条件。我无法返回 true
或 false
,我看到 async
函数是不同的,但尽管尝试了几次,但没有任何效果。
Controller.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;
关于mysql - 如何从 findOne 函数 MySQL 返回一个 bool 值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64662487/