我有一个简单的类,其中的方法可以使插入数据变得更容易。我正在使用 pg-promise 库与数据库对话。
class C_SQL
{
insert_text(text)
{
var time_now = new Date();
return
db.none(
`INSERT INTO notes(\
id,\
date,\
text,\
)
VALUES (\
(SELECT max(id) from notes)+1\,
'${time_now.setFullYear(time_now.getFullYear() + 1) ? time_now : ``}',\
${text}\
);`
);
}
}
我尝试像这样使用它:
var one = new C_SQL();
one.insert_text("inserted from a program")
.then(() => console.log("Inserted"))
.catch(err => console.log(err));
我一定做错了什么,导致 insert_text
方法不返回任何 promise 。我收到以下错误:
.then(() => console.log("Inserted"))
^
TypeError: Cannot read property 'then' of undefined
...
尝试通用db.query(...)
会产生同样的问题。但是,在我的代码的其他部分(在方法内的类似类中),db.query("SELECT...
按预期工作。
最佳答案
JavaScript 有一个名为 “automatic semicolon insertion” 的错误功能这就是转变:
return
db.none(
…
);
进入此:
return;
db.none(
…
);
您可以通过在 return
之后不直接换行来修复它,并通过运行 linter 来防止它 – ESLint ,例如 – 作为开发过程的一部分。
return db.none(
…
);
关于javascript - 类型错误 : Cannot read property 'then' of undefined with pg-promise insertion,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43839022/