nodejs mysql2 异步错误(中间值)不可迭代
nodejs mysql2 async error (intermediate value) is not iterable
我的路线是这样的
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)
}
})
我正在尝试查询用户 table 以检查用户是否存在。使用 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';
我的路线是这样的
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)
}
})
我正在尝试查询用户 table 以检查用户是否存在。使用 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';