续集如何检查数据库中是否存在条目

时间:2022-09-26 00:13:01

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

我需要使用Node.js中的Sequelize检查数据库中是否存在具有特定ID的条目

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

I call this function in an if statement but the result is always undefined

我在if语句中调用此函数,但结果始终未定义

if(isIdUnique(id)){...}

4 个解决方案

#1


8  

You are not returning from the isIdUnique function:

您没有从isIdUnique函数返回:

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) {
        // ...
    }
});

#2


7  

I don't prefer using count to check for record existence. Suppose you have similarity for hundred in million records why to count them all if you want just to get boolean value, true if exists false if not?

我不喜欢用count来检查记录的存在。假设你有几百万条记录的相似性,为什么要计算它们全部如果你想要得到布尔值,如果存在则为真,如果不存在则为假?

findOne will get the job done at the first value when there's matching.

当匹配时,findOne将以第一个值完成工作。

const isIdUnique = id =>
  db.Profile.findOne({ where: { id} })
    .then(token => token !== null)
    .then(isUnique => isUnique);

#3


1  

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:

由于Sequelize是围绕承诺设计的,alecxe的答案可能最有意义,但为了提供替代方案,你也可以传递一个回调:

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

isIdUnique(id, function(isUnique) {
  if (isUnique) {
    // stuff
  }
});

#4


0  

You can count and find.

你可以数数和找到。

    Project
  .findAndCountAll({
     where: {
        title: {
          [Op.like]: 'foo%'
        }
     },
     offset: 10,
     limit: 2
  })
  .then(result => {
    console.log(result.count);
    console.log(result.rows);
  });

Doc link, v5 Beta Release

Doc link,v5 Beta版

#1


8  

You are not returning from the isIdUnique function:

您没有从isIdUnique函数返回:

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) {
        // ...
    }
});

#2


7  

I don't prefer using count to check for record existence. Suppose you have similarity for hundred in million records why to count them all if you want just to get boolean value, true if exists false if not?

我不喜欢用count来检查记录的存在。假设你有几百万条记录的相似性,为什么要计算它们全部如果你想要得到布尔值,如果存在则为真,如果不存在则为假?

findOne will get the job done at the first value when there's matching.

当匹配时,findOne将以第一个值完成工作。

const isIdUnique = id =>
  db.Profile.findOne({ where: { id} })
    .then(token => token !== null)
    .then(isUnique => isUnique);

#3


1  

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:

由于Sequelize是围绕承诺设计的,alecxe的答案可能最有意义,但为了提供替代方案,你也可以传递一个回调:

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

isIdUnique(id, function(isUnique) {
  if (isUnique) {
    // stuff
  }
});

#4


0  

You can count and find.

你可以数数和找到。

    Project
  .findAndCountAll({
     where: {
        title: {
          [Op.like]: 'foo%'
        }
     },
     offset: 10,
     limit: 2
  })
  .then(result => {
    console.log(result.count);
    console.log(result.rows);
  });

Doc link, v5 Beta Release

Doc link,v5 Beta版