API解析后未发送/api/users/create响应,这可能导致请求停滞. NEXTJS [英] API resolved without sending a response for /api/users/create, this may result in stalled requests. NEXTJS
本文介绍了API解析后未发送/api/users/create响应,这可能导致请求停滞. NEXTJS的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我创建了一个API端点来处理我的Next.js应用程序中的用户帐户创建,并且我正在使用 knex.js 处理我的查询,但仍然出现此错误:
I created an API endpoint to handle user account creation in my Next.js app, and I am using knex.js to handle my queries, but I still get this error:
API resolved without sending a response for /api/users/create, this may result in stalled requests.
我在/pages/api/users/create.js
中的代码:
import { hash } from 'bcrypt';
import knex from '../../../knex';
export default async function regiterUser(req, res) {
if (req.method === 'POST') {
try {
hash(req.body.password, 10, async function (_, hash) {
await knex('users').insert({
name: req.body.name,
email: req.body.email,
role: 'user',
allowed: true,
password: hash,
});
res.end();
});
} catch (err) {
res.status(err).json({});
}
} else {
res.status(405);
}
}
推荐答案
bcrypt hash 函数实际上是一个异步函数,它返回一个承诺,可以用加密的数据盐解析,也可以拒绝.错误.
actually the bcrypt hash function is an async function, it returns a promise to be either resolved with the encrypted data salt or rejected with an Error.
import knex from '../../../knex';
export default async function regiterUser(req, res) {
if (req.method === 'POST') {
try {
const hashed = await hash(req.body.password, 10);
await knex('users').insert({
name: req.body.name,
email: req.body.email,
role: 'user',
allowed: true,
password: hashed,
});
res.status(200).end();
} catch (err) {
res.status(err).json({});
}
} else {
res.status(405);
res.end();
}
}
这篇关于API解析后未发送/api/users/create响应,这可能导致请求停滞. NEXTJS的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文