question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Element implicitly has an 'any' type because expression of type '0' can't be used to index type 'RowDataPacket[] | RowDataPacket[][] | OkPacket | OkPacket[] | ResultSetHeader'. Property '0' does not exist on type 'RowDataPacket[] | RowDataPacket[][] | OkPacket | OkPacket[] | ResultSetHeader'. When trying to access any index of the result parameter in the callback

See original GitHub issue

Hi there so I have this code

    static async getById(id: string): Promise<Business> {
        return new Promise((resolve, reject) => {
            connection.execute(
                "SELECT * FROM business WHERE ID = ?;",
                [id],
                (err, result) => {
                    if (err) reject(err);
                    const { id, name, email, password }: {id: string, name: string, email: string, password: string} = result[0]; //it complains here
                    resolve({
                        id,
                        name,
                        email,
                        password,
                    });
                }
            );
        });
    }

And I have typescript giving me this error:

Element implicitly has an 'any' type because expression of type '0' can't be used to index type 'RowDataPacket[] | RowDataPacket[][] | OkPacket | OkPacket[] | ResultSetHeader'.
  Property '0' does not exist on type 'RowDataPacket[] | RowDataPacket[][] | OkPacket | OkPacket[] | ResultSetHeader'.ts(7053)

And I’m really not sure what to be doing here because I can’t change return type of result

Issue Analytics

  • State:open
  • Created 2 years ago
  • Reactions:1
  • Comments:5 (2 by maintainers)

github_iconTop GitHub Comments

3reactions
astiobcommented, Apr 19, 2022

I’m using .execute<RowDataPacket[]>(...) for single-statement selects. Similarly, multi-statement selects would have .execute<RowDataPacket[][]>(...).

I don’t know whether the right type for inserts, updates, creates etc. is ResultSetHeader (which I’m getting in practice) or OkPacket | ResultSetHeader, though.

2reactions
sasial-devcommented, Jan 23, 2022

+1, also an issue here got around it by typecasting, but I don’t know the library/mysql well so it’s not ideal.

			const [matches] = await db.Connection.query(
				"SELECT `Discord UID` FROM `Information` WHERE `Staff Code` = ? OR `Discord UID` = ? OR `Roblox UID` = ? OR `Email`= ?",
				[`${staffInfo.department}-${staffInfo.staffid}`, staffInfo.user.id, userId, staffInfo.email],
			);
			console.log((matches as RowDataPacket[])[0]);

this is with “mysql2/promise”

Read more comments on GitHub >

github_iconTop Results From Across the Web

The type error about the return of mysql2/promise query
TS7053: Element implicitly has an 'any' type because expression of type '0' can't be used to index type 'RowDataPacket[] | RowDataPacket[][] | ...
Read more >
element implicitly has an 'any' type because ... - You.com
TS7053: Element implicitly has an 'any' type because expression of type '0' can't be used to index type 'MathType'. Property '0' does not...
Read more >
How to access a RowDataPacket object | Edureka Community
I'm currently developing a desktop application with Node-webkit. During that process I need to get some data from a local MySQL-database.
Read more >
TypeScript errors and how to fix them
If you want to support multiline text, then you would have to use string ... error TS1055: Type ' AxiosPromise ' is not...
Read more >
Node.js MySQL SELECT FROM Query Examples - Tutorial Kart
js program to access data of a MySQL Table. MySQL SELECT Query is used to get one or more rows from MySQL Table....
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found