我的nodejs应用程序需要检查是否findOne找到任何匹配项。
let o = await Model.findOne({where : {name: 'myname'}});
if (no match in o ) {
//do something
} else {
//do something else
};
Run Code Online (Sandbox Code Playgroud)
但是我没有找到任何文件解释findOne没有匹配时返回的内容。我知道它不会返回null或undefined。返回是一个对象,我怎么知道没有匹配项。
按照DOC和下面的示例,在这里您将返回记录或返回null(对于未找到记录):
// 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
})
Run Code Online (Sandbox Code Playgroud)
因此,就您而言,您可以像这样:
let o = await Model.findOne({where : {name: 'myname'}});
if (o) {
// Record Found
} else {
// Not Found
};
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1094 次 |
| 最近记录: |