类型错误:无法使用 pg 承诺插入读取未定义的属性'then'



>我有一个简单的类,它有一个使插入数据更容易的方法。我正在使用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方法不返回任何承诺。我收到以下错误:

    .then(() => console.log("Inserted"))
    ^
TypeError: Cannot read property 'then' of undefined
...

尝试通用db.query(...)会产生相同的问题。但是,在我的代码的其他部分(在方法内部的类似类中(,db.query("SELECT...按预期工作。

JavaScript 有一个叫做"自动分号插入"的错误功能,它正在改变这个:

return
db.none(
    …
);

进入这个:

return;
db.none(
    …
);

您可以通过在 return 之后直接不使用换行符来修复它,并通过运行 linter(例如 ESLint(作为开发过程的一部分来防止它。

return db.none(
    …
);

最新更新