gpt4 book ai didi

node.js - 非索引字段上的索引重复键

转载 作者:太空宇宙 更新时间:2023-11-04 00:03:08 25 4
gpt4 key购买 nike

我有这个用户架构:

var mongoose = require("mongoose"), 
passportLocalMongoose = require("passport-local-mongoose");

let userSchema = new mongoose.Schema({
username:
{type: String,
unique: true
},
password: String,
privileges:
[{
region: Number,
read: Number,
write: Number,
edit: Number
}]
});

userSchema.plugin(passportLocalMongoose);

module.exports = mongoose.model("User", userSchema);

用这个html

document.addEventListener("DOMContentLoaded", function()
{
window.x = document.querySelector(".set-nou-privilegii");
x.addEventListener("click", adaugarePrivilegii);
});

var y = 1;
var x = document.querySelector(".set-nou-privilegii");
function adaugarePrivilegii(e)
{
e.preventDefault();
x.insertAdjacentHTML("beforebegin",
`<br/>
<label for="region">Regiune:</label>
<input type="Number" name="privileges[${y}][region]" id="region-inregistrare" placeholder="cod regiune"><br/>
<label for="citire">Citire (0 - nu, 1 - da):</label>
<input type="Number" name="privileges[${y}][read]" id="citire-inregistrare" placeholder="1-acces, 0-nu"><br/>
<label for="scriere">Scriere (0 - nu, 1 - da):</label>
<input type="Number" name="privileges[${y}][write]" id="scriere-inregistrare" placeholder="1-acces, 0-nu"><br/>
<label for="editare">Editare (0 - nu, 1 - da):</label>
<input type="Number" name="privileges[${y}][edit]" id="editare-inregistrare" placeholder="1-acces, 0-nu"><br/>`)
y++;
}
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
<meta name="viewport" content="width=device-width,initial-scale=1">
<script src="/javascript/js-inregistrare.js"></script>
<link rel="stylesheet" href="reset.css">
<link href="https://fonts.googleapis.com/css?family=Roboto" rel="stylesheet">
<link rel="stylesheet" href="css.css">
<title>Inregistrari</title>
</head>
<body>
<div class="center">
<img src="/img/logo.jpg" class="logo-inregistrare">
<form action="/register" method="POST" class="form-inregistrare">
<label for="username">Nume utilizator:</label>
<input type="text" name="username" id="username-inregistrare" placeholder="nume utilizator"><br/>
<label for="password">Parola:</label>
<input type="password" name="password" id="password-inregistrare" placeholder="parola"><br/>
<label for="region">Regiune:</label>
<input type="Number" name="privileges[0][region]" id="region-inregistrare" placeholder="cod regiune"><br/>
<label for="citire">Citire (0 - nu, 1 - da):</label>
<input type="Number" name="privileges[0][read]" id="citire-inregistrare" placeholder="1-acces, 0-nu"><br/>
<label for="scriere">Scriere (0 - nu, 1 - da):</label>
<input type="Number" name="privileges[0][write]" id="scriere-inregistrare" placeholder="1-acces, 0-nu"><br/>
<label for="editare">Editare (0 - nu, 1 - da):</label>
<input type="Number" name="privileges[0][edit]" id="editare-inregistrare" placeholder="1-acces, 0-nu"><br/>
<button class="set-nou-privilegii">Adaugare set nou privilegii</button><br/>
<button class="trimite-inregistrare">Trimite</button><br/>
</form>
</div>
</body>
</html>

以及这些路线

var express = require("express"),
router = express.Router(),
User = require("../models/user.js"),
passport = require("passport"),
middleware = require("./middleware.js");

router.get('/register', middleware.isLoggedIn, function(req, res)
{
res.render("inregistrare");
});

router.post('/register', middleware.isLoggedIn, function(req, res)
{
User.register(new User({
username: req.body.username,
privileges: req.body.privileges
}), req.body.password, function(err)
{
if(err)
{
console.log(err);
res.redirect("/register");
}
else
{
passport.authenticate("local")(req, res, function() {
res.redirect("/");
});
}
})
});

router.get('/login', middleware.isLoggedOut, function(req, res)
{
var title = "title"
res.render("login", {title: title});
});

router.post("/login", middleware.isLoggedOut, passport.authenticate("local", {
successRedirect: "/register",
failureRedirect: "/login"
}), function(req, res) {
});

module.exports = router;

当我注册一个新用户时(如果当时数据库为空),一切都很好。如果我尝试使用相同的区域 ID 注册另一个用户,我会得到以下信息: photo

我尝试使用不同的区域 ID 和相同的字段进行写入、编辑和读取,并且它有效。只有当我的区域具有相同的 id 时,我才会收到此消息,但我不明白为什么。 Region 未声明为索引或 unique: true 那么为什么会发生这种情况?

我该如何解决这个问题?

最佳答案

看起来region曾经被声明为unique: true,它在该字段上创建了唯一索引。 Mongoose 不会根据架构修改或删除现有索引,因此您需要手动删除过时的索引。

在外壳中:

db.users.dropIndex('privileges.region_1')

关于node.js - 非索引字段上的索引重复键,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53730961/

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