Sequelize how to check if entry exists in database

后端 未结 5 581
伪装坚强ぢ
伪装坚强ぢ 2021-01-01 15:33

I need to check if entry with specific ID exists in the database using Sequelize in Node.js

  function isIdUnique (id) {
    db.Profile.count({ where: { id:          


        
5条回答
  •  旧时难觅i
    2021-01-01 16:10

    As Sequelize is designed around promises anyway, alecxe's answer probably makes most sense, but for the sake of offering an alternative, you can also pass in a callback:

    function isIdUnique (id, done) {
        db.Profile.count({ where: { id: id } })
          .then(count => {
            done(count == 0);
          });
      }
    }
    
    isIdUnique(id, function(isUnique) {
      if (isUnique) {
        // stuff
      }
    });
    

提交回复
热议问题