TransWikia.com

Sequelize query reply is: ,[object Object]

Stack Overflow Asked on December 16, 2020

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…

One Answer

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)

Correct answer by Tim on December 16, 2020

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP