user8236024
user8236024

Reputation:

mongoose nodejs "Unknown modifier: $pushAll"

Hello I am taking this course, but I can't seem to fix my problem, I get { "name": "MongoError", "message": "Unknown modifier: $pushAll", "driver": true, "index": 0, "code": 9, "errmsg": "Unknown modifier: $pushAll" } when I try to make a new user. I looked it up and it said too add { usePushEach: true }); to my mongoose's schema settings which I did, but it still errors and I can't seem to fix it here is the code

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};

Upvotes: 4

Views: 4001

Answers (2)

Orelsanpls
Orelsanpls

Reputation: 23565

Looking at mongodb official documentation $pushAll had been deprecated since v2.4.

In latest mongodb version (3.6) $pushAll does not exist anymore.

Use the $push operator with $each instead.


If you want to force the use of $pushAll, a solution is given in this thread

enter image description here


Real answer to the problem is :

@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

Upvotes: 7

Rahul Sharma
Rahul Sharma

Reputation: 10111

Instead of

 tokens: [{
    access: {type: String, require: true},
    token: {type: String, require: true}
  }]

try this

 tokens: {type: Array, require: true}

Upvotes: 0

Related Questions