0

当您尝试使用 .prepare(#).get() 并且查询不存在时,我该如何处理发生的错误?

let businessType = DB.prepare(`SELECT businessType from 'Profiles' WHERE userId = '${author.id}'`).get().businessType;

所以基本上我如何在“businessType”不存在时停止崩溃,而不是脚本崩溃,我可以发送一条消息,如“用户不存在”之类的。

提前致谢!

4

1 回答 1

0

get()undefined如果该行不存在,将返回。

在尝试提取数据之前,您需要检查您的回复。

const row = DB.prepare(`SELECT businessType from 'Profiles' WHERE userId = ?`).get(author.id);
if(row) {
  // profile recorded exist
  ...
} else {
  // profile doesn't exist.
  ...
}

于 2021-10-11T08:30:45.083 回答