mysql - 如何从 findOne 函数 MySQL 返回一个 bool 值

标签 mysql node.js asynchronous async-await sequelize.js

我想使用 findOne 的结果作为 if 语句的条件。我无法返回 truefalse ,我看到 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/

相关文章:

java - Hibernate 中对所有表的所有查询的通用条件限制

java - 使用 JavaMail 在超过截止日期时自动向用户发送电子邮件

node.js - nodejs并发下如何正确使用redis的GET SET命令?

javascript - 在 node.js 中使用多个异步 redis 调用正确放置回调

ios - 填充 UITableView 的更快方法?

mysql 连接多个

php - SQL 的删除按钮

node.js - Mongoose 返回空 JSON 数组

ios-deploy 无法在 Mac OS X El Capitan 10.11 上安装

javascript - React/Redux 异步操作