【发布时间】:2021-06-02 20:19:06
【问题描述】:
我正在开发一个使用数据库并使用用户 ID 向用户添加项目的应用程序,用户 ID 是用户名,但是当我尝试添加项目时出现错误,我似乎无法弄清楚如何修理它。这是错误:
(node:14352) UnhandledPromiseRejectionWarning: MongoError: E11000 duplicate key error collection: sermontracker.items index: _id_ dup key: { _id: "sdfa3asdf2@asdf" }
at Function.create (C:\Users\Children\Desktop\Web Projects\Sermon_Tracker\node_modules\mongodb\lib\core\error.js:57:12)
at toError (C:\Users\Children\Desktop\Web Projects\Sermon_Tracker\node_modules\mongodb\lib\utils.js:123:22)
at C:\Users\Children\Desktop\Web Projects\Sermon_Tracker\node_modules\mongodb\lib\operations\common_functions.js:258:39
at handler (C:\Users\Children\Desktop\Web Projects\Sermon_Tracker\node_modules\mongodb\lib\core\sdam\topology.js:943:24)
at C:\Users\Children\Desktop\Web Projects\Sermon_Tracker\node_modules\mongodb\lib\cmap\connection_pool.js:350:13
at handleOperationResult (C:\Users\Children\Desktop\Web Projects\Sermon_Tracker\node_modules\mongodb\lib\core\sdam\server.js:558:5)
at MessageStream.messageHandler (C:\Users\Children\Desktop\Web Projects\Sermon_Tracker\node_modules\mongodb\lib\cmap\connection.js:277:5)
at MessageStream.emit (events.js:315:20)
at processIncomingData (C:\Users\Children\Desktop\Web Projects\Sermon_Tracker\node_modules\mongodb\lib\cmap\message_stream.js:144:12)
at MessageStream._write (C:\Users\Children\Desktop\Web Projects\Sermon_Tracker\node_modules\mongodb\lib\cmap\message_stream.js:42:5)
at doWrite (_stream_writable.js:403:12)
at writeOrBuffer (_stream_writable.js:387:5)
at MessageStream.Writable.write (_stream_writable.js:318:11)
at Socket.ondata (_stream_readable.js:716:22)
at Socket.emit (events.js:315:20)
at addChunk (_stream_readable.js:295:12)
(node:14352) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). To terminate the node process on unhandled promise rejection, use the CLI flag `--unhandled-rejections=strict` (see https://nodejs.org/api/cli.html#cli_unhandled_rejections_mode). (rejection id: 1)
(node:14352) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.
这是错误发生的时间和地点的代码:
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");
});
这里是完整的代码:
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");
});
代码看起来可以正常工作,但实际上并没有。每次我尝试向用户添加另一个项目时,它都会给我错误。
【问题讨论】:
-
E11000 duplicate key error表示将违反索引的唯一性约束。_id索引始终是唯一的,因此如果您尝试插入第二个具有相同_id值的文档,您将收到该错误。 -
好的,谢谢,我将使用不同的名称,而不是使用
_id。感谢您提供的信息,我会记住这一点。这回答了我的问题。
标签: javascript html node.js mongodb mongoose