简体   繁体   中英

NodeJS mysql's Textrow returns undefined even though it's a valid object

I have a funciton called getSpidFromOvr()

const mysql = require('mysql2/promise');
async function gopher(param1, param2){
    let param3 = parseInt(param1 - ovrUpgrade[param2 - 1]);

    const db = await mysql.createConnection({
        host: 'localhost',
        user: 'root',
        database: 'default_schema'
    });

    db.connect();
    let res = await db.query({
        sql: "SELECT `id` FROM `players` WHERE `param` = ? LIMIT 1",
        values: [param3]
    });
    db.end();
    return await res;
}

which I call in my main() function,

function main(){
    // First, get the ovr
    gopher(101, 5)
    .then((res) => {
        let row = res[0]; // [ TextRow { id: 100 } ]
        console.log(row.id); // Returns undefined
    })
}

Why is it that row returns a valid object (using typeof row returns object ) but when I try to access the element id of row , it returns undefined ?

Figured it out. Instead of doing

res[0].id

I did

res[0][0].id

Certainly a bit weird since I was able to call res[0].id if I were to use the callback function, but hope this helps anyone who comes across this issue.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM