Koa js : 错误:发送后无法设置 headers。 mongoDB
Koa js : Err: Can't set headers after they're sent. mongoDB
我正在使用 Koa 2 的异步函数。
我收到以下错误。
events.js:163
1:56 PM
throw er; // Unhandled 'error' event
1:56 PM
^
1:56 PM
1:56 PM
Error: Can't set headers after they are sent.
1:56 PM
at ServerResponse.setHeader (_http_outgoing.js:371:11)
1:56 PM
at Object.set (/app/node_modules/koa/lib/response.js:440:16)
1:56 PM
at Object.redirect (/app/node_modules/koa/lib/response.js:261:10)
1:56 PM
at Object.proto.(anonymous function) [as redirect] (/app/node_modules/delegates/index.js:40:31)
1:56 PM
Jump to
at url.findOne (/app/server.js:126:9)
1:56 PM
at model.Query. (/app/node_modules/mongoose/lib/model.js:3737:16)
1:56 PM
at /app/node_modules/kareem/index.js:277:21
1:56 PM
at /app/node_modules/kareem/index.js:131:16
1:56 PM
at _combinedTickCallback (internal/process/next_tick.js:73:7)
1:56 PM
at process._tickCallback (internal/process/next_tick.js:104:9)
1:56 PM
6 minutes ago
这是我的代码的一部分。
async function red(ctx) {
let redurl = "//url here";
url.findOne({ shortenedLink: redurl }, (err, data) => {
//find if short url matches long url in db
if (err) throw err;
if (data) {
//if matches then redirect to long url
ctx.redirect(data.url); //getting the error here
console.log("matched");
} else console.error("--");
});
}
可以找到我的完整代码 here。
如果您使用的是 async
函数,则应遵循 async
/`await?模式而不是使用回调。所以你应该像这样重写你的电话:
async function red(ctx) {
let redurl = "//url here";
try {
data = await url.findOne({ shortenedLink: redurl })
if (data) {
ctx.redirect(data.url); //getting the error here
console.log("matched");
} else {
console.error("--");
}
} catch (err) {
throw err;
}
}
我遇到了同样的问题,因为我在我的代码中使用了链式 sendStatus
函数,如前所述。
Country.count({ name: name }, (error, count) => {
if (count > 0) {
console.log(count);
error = name + alreadyExists;
res.sendStatus(500).sendStatus(500);
}
});
删除多余的 sendStatus
解决了我的问题。
Country.count({ name: name }, (error, count) => {
if (count > 0) {
console.log(count);
error = name + alreadyExists;
res.sendStatus(500);
}
});
我正在使用 Koa 2 的异步函数。
我收到以下错误。
events.js:163 1:56 PM throw er; // Unhandled 'error' event 1:56 PM ^ 1:56 PM 1:56 PM Error: Can't set headers after they are sent. 1:56 PM at ServerResponse.setHeader (_http_outgoing.js:371:11) 1:56 PM at Object.set (/app/node_modules/koa/lib/response.js:440:16) 1:56 PM at Object.redirect (/app/node_modules/koa/lib/response.js:261:10) 1:56 PM at Object.proto.(anonymous function) [as redirect] (/app/node_modules/delegates/index.js:40:31) 1:56 PM Jump to at url.findOne (/app/server.js:126:9) 1:56 PM at model.Query. (/app/node_modules/mongoose/lib/model.js:3737:16) 1:56 PM at /app/node_modules/kareem/index.js:277:21 1:56 PM at /app/node_modules/kareem/index.js:131:16 1:56 PM at _combinedTickCallback (internal/process/next_tick.js:73:7) 1:56 PM at process._tickCallback (internal/process/next_tick.js:104:9) 1:56 PM 6 minutes ago
这是我的代码的一部分。
async function red(ctx) {
let redurl = "//url here";
url.findOne({ shortenedLink: redurl }, (err, data) => {
//find if short url matches long url in db
if (err) throw err;
if (data) {
//if matches then redirect to long url
ctx.redirect(data.url); //getting the error here
console.log("matched");
} else console.error("--");
});
}
可以找到我的完整代码 here。
如果您使用的是 async
函数,则应遵循 async
/`await?模式而不是使用回调。所以你应该像这样重写你的电话:
async function red(ctx) {
let redurl = "//url here";
try {
data = await url.findOne({ shortenedLink: redurl })
if (data) {
ctx.redirect(data.url); //getting the error here
console.log("matched");
} else {
console.error("--");
}
} catch (err) {
throw err;
}
}
我遇到了同样的问题,因为我在我的代码中使用了链式 sendStatus
函数,如前所述。
Country.count({ name: name }, (error, count) => {
if (count > 0) {
console.log(count);
error = name + alreadyExists;
res.sendStatus(500).sendStatus(500);
}
});
删除多余的 sendStatus
解决了我的问题。
Country.count({ name: name }, (error, count) => {
if (count > 0) {
console.log(count);
error = name + alreadyExists;
res.sendStatus(500);
}
});