0

The function below worked but has broken without changes

exports.updateFormState = async() => {
    let queryReply;
    try {
        queryReply = await db.sequelize.query(
            "SELECT * FROM stackoverflow"
        );
        console.log("Im passed the reply")
        console.log("This is the query: " + queryReply)
        if (queryReply[1] === 1) {
            console.log("Inside if statement")
            return "SUCCESS";
        }
    }catch(err) {
        return err.message;
    }
}

What it should do

It needs to fire the query to the database and return the results.

What it does

It returns ,[object Object] The weird thing is that it always returned the right thing but now it doesn't

Things that work

The query works (Not shown in code) The database connection is there It worked before...

1
  • 2
    change the console.log("This is the query: " + queryReply) to console.log("This is the query: " , queryReply) Commented Nov 6, 2020 at 9:16

1 Answer 1

1

Snippet from the Sequelize docs:

By default the function will return two arguments - a results array, and an object containing metadata (such as amount of affected rows, etc). Note that since this is a raw query, the metadata are dialect specific. Some dialects return the metadata "within" the results object (as properties on an array). However, two arguments will always be returned, but for MSSQL and MySQL it will be two references to the same object.

So, this will slightly depend on your underlying database of choice, but you're more than likely going to want to return queryReply[0] rather than queryReply.

Additionally, I note that you're doing this:

console.log("This is the query: " + queryReply)

This will attempt to render queryReply as a string, which it isn't, since it's an object. Instead, you might find it easier to debug using:

console.log("This is the query: ", queryReply)
Sign up to request clarification or add additional context in comments.

2 Comments

This actually works thanks. But I have used the console.log("This is the query: " + queryReply) before and that has worked. Would you know why?
My best guess would be that your environment has recently updated from a previous version of Sequelize to v2 or later. Please check out the changelog, and have a look at the notes for v2.0.0 when the .query() function was changed to return two params. :) Happy hacking.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.