1

我担心这段代码:

knex('person').where('id','=', 1).then(result => {
    const person = result[0]; // the code I worry - result should contain only ONE element
});

如果我使用 C#,我会使用 Linq 的 Single,如果结果中有多个元素,它会快速失败并抛出异常,比如忘记 where 子句或条件错误。

我将如何在 JavaScript 中实现它?

4

0 回答 0