gpt4 book ai didi

node.js - Node Express js 未处理的拒绝错误 [ERR_HTTP_HEADERS_SENT]

转载 作者:行者123 更新时间:2023-12-03 22:38:26 26 4
gpt4 key购买 nike

我是 Node js 的新手。我正在尝试将数据插入数据库,在此之前我做了一些验证以检查数据库中已存在的用户名。请参阅我使用的编码。

var express = require("express");
var router = express.Router();
var models = require("../models");

const { check, validationResult } = require('express-validator/check');

/* GET users listing. */
router.get('/', function(req, res, next) {
res.send('respond with a resource - merchant');
});

/* POST ADD USERS. */
router.post('/add-merchant', [
check('name').not().isEmpty(),
], (req, res, next) => {
try {
const errors = validationResult(req);

if (!errors.isEmpty()) {
return res.json({ errors: errors.array()});
}

console.log("error passed")

models.merchants.findOne({ where: {name: req.body.name}}).then((merchant) => {
if(merchant) {
throw new Error("Username already exists")
}
models.merchants.create(req.body).then((merchant) => {
res.json(merchant)
});
})
} catch (error) {
res.json({"status": "error", "message": error.message})
}
});

module.exports = router;

运行上述代码时出现以下错误
Unhandled rejection Error [ERR_HTTP_HEADERS_SENT]: Cannot set headers after they are sent to the client
at ServerResponse.setHeader (_http_outgoing.js:470:11)
at ServerResponse.header (/home/assistanz/Desktop/node/gb360/node_modules/express/lib/response.js:767:10)
at ServerResponse.send (/home/assistanz/Desktop/node/gb360/node_modules/express/lib/response.js:170:12)
at ServerResponse.json (/home/assistanz/Desktop/node/gb360/node_modules/express/lib/response.js:267:15)
at models.merchants.create.then (/home/assistanz/Desktop/node/gb360/routes/merchant.js:33:21)
at tryCatcher (/home/assistanz/Desktop/node/gb360/node_modules/bluebird/js/release/util.js:16:23)
at Promise._settlePromiseFromHandler (/home/assistanz/Desktop/node/gb360/node_modules/bluebird/js/release/promise.js:512:31)
at Promise._settlePromise (/home/assistanz/Desktop/node/gb360/node_modules/bluebird/js/release/promise.js:569:18)
at Promise._settlePromise0 (/home/assistanz/Desktop/node/gb360/node_modules/bluebird/js/release/promise.js:614:10)
at Promise._settlePromises (/home/assistanz/Desktop/node/gb360/node_modules/bluebird/js/release/promise.js:694:18)
at _drainQueueStep (/home/assistanz/Desktop/node/gb360/node_modules/bluebird/js/release/async.js:138:12)
at _drainQueue (/home/assistanz/Desktop/node/gb360/node_modules/bluebird/js/release/async.js:131:9)
at Async._drainQueues (/home/assistanz/Desktop/node/gb360/node_modules/bluebird/js/release/async.js:147:5)
at Immediate.Async.drainQueues [as _onImmediate] (/home/assistanz/Desktop/node/gb360/node_modules/bluebird/js/release/async.js:17:14)
at runCallback (timers.js:705:18)
at tryOnImmediate (timers.js:676:5)
at processImmediate (timers.js:658:5)

请帮助任何人解决这个问题。

提前致谢。

最佳答案

发生此错误是因为您返回了响应并且还有一些东西要加载
你必须删除 console.log("error passed") 或者你可以把它放在 return 语句之前..
并为每个 return 语句编写 res.json 语句
作为

   return res.json({})

关于node.js - Node Express js 未处理的拒绝错误 [ERR_HTTP_HEADERS_SENT],我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54603955/

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