Sequelize how to check if entry exists in database

后端 未结 5 596
伪装坚强ぢ
伪装坚强ぢ 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条回答
  •  孤独总比滥情好
    2021-01-01 16:20

    Update: see the answer which suggests using findOne() below. I personally prefer; this answer though describes an alternative approach.

    You are not returning from the isIdUnique function:

    function isIdUnique (id) {
        return db.Profile.count({ where: { id: id } })
          .then(count => {
            if (count != 0) {
              return false;
            }
            return true;
        });
    }
    
    isIdUnique(id).then(isUnique => {
        if (isUnique) {
            // ...
        }
    });
    

提交回复
热议问题