gpt4 book ai didi

node.js - Mongoose nodejs "Unknown modifier: $pushAll"

转载 作者:可可西里 更新时间:2023-11-01 09:25:42 26 4
gpt4 key购买 nike

你好,我正在学习这门类(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.


如果想强制使用$pushAllthis thread中给出了解决方案

enter image description here


问题的真正答案是:

@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/

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