你好,我正在学习这门类(class),但我似乎无法解决我的问题,我明白了
{
"名称": "MongoError",
"message": "未知修饰符:$pushAll",
“司机”:是的,
“指数”:0,
“代码”:9,
"errmsg": "未知修饰符:$pushAll"
当我尝试创建新用户时。我查了一下,上面说太添加了
{
usePushEach:真
});
我做的我的 Mongoose 模式设置,但它仍然出错,我似乎无法修复它这里是代码
const mongoose = require("mongoose");
const validator = require("validator");
const jwt = require("jsonwebtoken");
var UserSchema = new mongoose.Schema({
email: {
require: true,
type: String,
minlength: 1,
trim: true,
unique: true,
validate: {
validator: validator.isEmail,
message: `{VALUE} is not a valid email`
}
},
password: {
type: String,
require: true,
minlength: 6
},
tokens: [{
access: {type: String, require: true},
token: {type: String, require: true}
}]
},{
usePushEach: true
});
UserSchema.methods.generateAuthToken = function() {
var user = this;
var access = "auth";
var token = jwt.sign({_id: user._id.toHexString(), access}, "abc123").toString();
// user = user.concat({access, token})
// console.log(user)
user.tokens.push({access, token})
return user.save().then(() => {
return token
})
};
var User = mongoose.model("User", UserSchema);
module.exports = {User};
最佳答案
查看mongodb official documentation $pushAll
自 v2.4 以来已被弃用。
在最新的 mongodb 版本 (3.6) $pushAll
不再存在。
Use the $push operator with $each instead.
如果想强制使用$pushAll
,this thread中给出了解决方案
问题的真正答案是:
@SkylarLopez hm... I would look at your version of mongoDb and mongoose. See if they match, maybe you have an unappropriate mongoose version that use $pushAll in it .save method
关于node.js - Mongoose nodejs "Unknown modifier: $pushAll",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48659992/