Search code examples
node.jsexpressnode-mysql2

nodejs mysql2 async error (intermediate value) is not iterable


my route is like this

import express from 'express'
import mysql from 'mysql2'
import { dbusername } from '../constant/db'
import { dbpassword } from '../constant/db'
import { dbname } from '../constant/db'
import { host } from '../constant/db'
let router = express.Router()
router.post('/', async (request, res, next) => {
    try {
        const { email, password } = request.body
        const connection = await mysql.createConnection({
            host: host,
            user: dbusername,
            password: dbpassword,
            database: dbname,
        })
        const [
            rows,
            fields,
        ] = await connection.execute(
            'SELECT `id` FROM `users` WHERE `email`= ? AND `password` = ?',
            [email, password]
        )

        res.status(200).json({ status: true })
    } catch (error) {
        console.log('error async', error)
    }
})

I'm trying to query users table to check if a user exist. Using nodejs msql2 async wait. I got this error

error async TypeError: (intermediate value) is not iterable
    at /Users/cos/Desktop/admin/routes/login.js:21:7
    at processTicksAndRejections (internal/process/task_queues.js:93:5)

Solution

  • I think you need to import the promise version of mysql2:

    import mysql from 'mysql2/promise';