没有匹配项时,findOne返回什么?

时间:2018-11-06 04:15:18

标签: sequelize.js

我的nodejs应用需要检查findOne是否找到任何匹配项。

  let o = await Model.findOne({where : {name: 'myname'}});
  if (no match in o ) {
  //do something
  } else {
  //do something else
  };

但是我没有找到任何文档解释findOne在不匹配时返回的内容。我知道它不会返回nullundefined。返回是一个对象,我怎么知道没有匹配项。

1 个答案:

答案 0 :(得分:2)

根据 DOC ,在下面的示例中,它将返回记录或返回空值(对于未找到记录):

// search for attributes
Project.findOne({ where: {title: 'aProject'} }).then(project => {
  // project will be the first entry of the Projects table with the title 'aProject' || null
})

因此,在您的情况下,您可以这样做:

let o = await Model.findOne({where : {name: 'myname'}});
if (o) {
    // Record Found
} else {
    // Not Found
};
相关问题