【发布时间】: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");
});
【问题讨论】:
标签: javascript html node.js mongodb mongoose