Sunday, 7 May 2017

TypeError: Cannot read property 'then' of undefined with pg-promise insertion

I have a simple class with a method that makes inserting data easier. I'm using pg-promise library to talk to database.

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}\
            );`
        );
    }
}

I'm trying to use it like so:

var one = new C_SQL();
one.insert_text("I'm from a program")
    .then(() => console.log("Inserted"))
    .catch(err => console.log(err));

I must be doing something wrong, causing insert_text method to not return any promise. I'm getting the following error:

    .then(() => console.log("Inserted"))
    ^

TypeError: Cannot read property 'then' of undefined
...

Trying generic db.query(...) produces the same issue. However in other parts of my code (in a similar class inside method), db.query("SELECT... works as expected.



via Robert C. Holland

No comments:

Post a Comment