我该如何修复(节点:5796)UnhandledPromiseRejectionWarning:错误[ERR_HTTP_HEADERS_SENT]:错误?
Posted
技术标签:
????????????(??:5796)UnhandledPromiseRejectionWarning:??[ERR_HTTP_HEADERS_SENT]:?????????:How can i fix (node:5796) UnhandledPromiseRejectionWarning: Error [ERR_HTTP_HEADERS_SENT]: error? ??????:2021-06-02 18:17:23 ??????:?????? node js ????,???????????,?????????????,???????
(node:5796) UnhandledPromiseRejectionWarning: ?? [ERR_HTTP_HEADERS_SENT]: ? ServerResponse.header (C:\Users\Children\ Desktop\Web Projects\Sermon_Tracker\node_modules\express\lib\response.js:767:10) ? ServerResponse.send (C:\Users\Children\Desktop\Web Projects\Sermon_Tracker\node_modules\express\lib\response.js: 170:12) ? ServerResponse.json (C:\Users\Children\Desktop\Web Projects\Sermon_Tracker\node_modules\express\lib\response.js:267:15) ? C:\Users\Children\Desktop\Web Projects\ Sermon_Tracker\app.js:171:9 ? processTicksAndRejec?(??/??/task_queues.js:97:5)(??:5796)UnhandledPromiseRejectionWarning:????????????????? catch ?????????????,?????? .catch() ???????????? Promise ?????????,??? CLI ?? --unhandled-rejections=strict
(??? https://nodejs.org/api/cli.html#cli_unhandled_rejections_mode)? (?? id:1)(??:5796)[DEP0018] DeprecationWarning:????????????????,???? Promise ????????????? Node.js ???
??????????????,????????????????:
app.post('/signup', async (req, res) =>
// res.json( status: "ok" );
// const firstName = req.body.firstName;
// const lastName = req.body.lastName;
//const username = req.body.email;
//const password = req.body.password;
// user = email;
// const User1 = mongoose.model("User", userSchema);
// const user2 = new User1(
// first_name: firstName,
// last_name: lastName,
// email: email,
// password: password,
// );
// user2.save();
// console.log(user + "Done");
// res.redirect("/workspace");
// Hashing passwords
const username, password: plainTextPassword = req.body;
userN = username;
if (!username || typeof username !== 'string')
return res.json( status: 'error', error: 'Invalid username' );
if (!plainTextPassword || typeof plainTextPassword !== 'string')
return res.json( status: 'error', error: 'Invalid password' );
if (plainTextPassword.length < 5)
return res.json( status: 'error', error: 'Password too small. Should be atleast 6 characters long.' );
const password = await bcrypt.hash(plainTextPassword, 10);
try
const response = await User.create(
_id: userN,
username,
password
);
console.log('user created successfully: ', response);
res.redirect('/workspace');
catch (error)
if (error.code == 11000)
return res.json( status: 'error', error: 'Username already taken' );
throw error
const item1 = new Item(
_id: userN,
date: "Date",
loc: "Location",
title: "Title",
passage: "Passage",
file: "File"
);
defaultItems.push(item1);
res.json( status: 'ok' );
);
????????:
var userN;
var defaultItems = [];
mongoose.connect('mongodb://localhost:27017/sermontracker',
useNewUrlParser: true,
useUnifiedTopology: true,
useCreateIndex: true
);
const ItemSchema = new mongoose.Schema(
_id: String,
date: String,
loc: String,
title: String,
passage: String,
file: String
);
const Item = mongoose.model("Item", ItemSchema);
app.post("/login", async (req, res) =>
const username, password = req.body;
const user = await User.findOne( username ).lean();
userN = username;
if (!user)
return res.json( status: 'error', error: 'Invalid username/password' )
if (await bcrypt.compare(password, user.password))
const token = jwt.sign( id: user._id, username: user.username , JWT_SECRET);
res.redirect('/workspace');
res.json( status: 'ok', data: token );
res.json( status: 'ok', error: 'Invalid user/password' );
);
app.post('/signup', async (req, res) =>
const username, password: plainTextPassword = req.body;
userN = username;
if (!username || typeof username !== 'string')
return res.json( status: 'error', error: 'Invalid username' );
if (!plainTextPassword || typeof plainTextPassword !== 'string')
return res.json( status: 'error', error: 'Invalid password' );
if (plainTextPassword.length < 5)
return res.json( status: 'error', error: 'Password too small. Should be atleast 6 characters long.' );
const password = await bcrypt.hash(plainTextPassword, 10);
try
const response = await User.create(
_id: userN,
username,
password
);
console.log('user created succecfully: ', response);
res.redirect('/workspace');
catch (error)
if (error.code == 11000)
return res.json( status: 'error', error: 'Username already taken' );
throw error
res.json( status: 'ok' );
const item1 = new Item(
_id: userN,
date: "Date",
loc: "Location",
title: "Title",
passage: "Passage",
file: "File"
);
defaultItems.push(item1);
);
app.get("/", function (req, res)
res.render("home");
);
app.get("/change-password", function (req, res)
res.render("changepass");
);
app.get("/signup", function (req, res)
res.render("signup");
);
app.get("/login", function (req, res)
res.render("login");
);
app.get("/workspace", function (req, res)
Item.find( _id: userN , function (err, foundItems)
if (foundItems.length == 0)
Item.insertMany(defaultItems, function (err)
if (err)
console.log(err);
else
console.log("Added items");
);
res.redirect("/workspace");
else
res.render("workspace", itemList: foundItems );
);
);
app.post("/workspace", function (req, res)
const date = req.body.input1;
const location = req.body.input2;
const title = req.body.input3;
const passage = req.body.input4;
const file = req.body.input5;
const item = new Item(
_id: userN,
date: date,
loc: location,
title: title,
passage: passage,
file: file
);
item.save();
res.redirect("/workspace");
);
??????:
?????1?:??,userN
????????? (???????,??..)
??????,?????? JSON?????????????????????? JSON? (?????)??????????(????????:))
????:
????????????????userN
?????????????????????????????????
@LublubXT ?????????????????????????????? :)
??,?????,??????????????,??????????
??,??????
???????? throw ???? res.json( status: 'ok' );
?以上是关于我该如何修复(节点:5796)UnhandledPromiseRejectionWarning:错误[ERR_HTTP_HEADERS_SENT]:错误?的主要内容,如果未能解决你的问题,请参考以下文章
我该如何修复(节点:14352)UnhandledPromiseRejectionWarning:MongoError:E11000 重复键错误集合:错误?