gpt4 book ai didi

node.js - nodejs mysql2异步错误(中间值)不可迭代

转载 作者:行者123 更新时间:2023-12-05 02:04:19 24 4
gpt4 key购买 nike

我的路线是这样的

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)
}
})

我正在尝试查询用户表以检查用户是否存在。使用 nodejs msql2 异步等待。我遇到了这个错误

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)

最佳答案

我认为你需要导入mysql2的promise版本:

import mysql from 'mysql2/promise';

关于node.js - nodejs mysql2异步错误(中间值)不可迭代,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64410022/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com