我有这个用户模式:
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;
当我注册一个新用户时(如果此时DB是空的),一切都很好。如果我尝试使用相同的地区ID注册另一个用户,则会得到以下信息:
我尝试使用不同的区域ID和相同的字段进行写入,编辑和读取,并且有效。仅当我具有相同的区域ID时才得到此信息,但我不知道为什么。没有将Region声明为索引或不具有唯一性:true,为什么会发生?
我该如何解决?
答案 0 :(得分:1)
看起来像var value = [{'value': {'year': 2018, 'month':3, 'count': 123}}];
var result = [];
value.map(function(val,i){
for(var j in val){
result.push(val[j])
}
})
以前被声明为region
,这在该字段上创建了唯一索引。猫鼬不会基于架构修改或删除现有索引,因此您需要手动删除该过时的索引。
在外壳中:
unique: true