1

I am getting the correct data for friendRequests which is getting a user ID and throwing it in the friendRequest field of my mongoose file. When I add $push to add the data into the friendRequest array in the route file, it actually does not insert it and gives me back the err function I created.

Here is my route file:

exports.addContactPost = function(req, res, err) {
    User.findByIdAndUpdate(req.signedCookies.userid, {
                $push: {friendRequest: req.body.friendRequest}
            }, function(err) {
                if(err) {
                    console.log("post2");
                    return console.log('error');

                } 

                else {
                    console.log('postsuccess');
                    res.json({response: true});

                }

            });
};

Here is the mongoose file:

var mongoose = require('mongoose'),
    Schema = mongoose.Schema,
    bcrypt = require('bcrypt-nodejs'),
    SALT_WORK_FACTOR = 10;


var UserSchema = new Schema({ 
    email: { type: String, required: true, lowercase:true, index: { unique: true } },
    password: { type: String, required: true },
    firstName: {type: String, required: true},
    lastName: {type: String, required: true},
    phone: {type: Number, required: true},
    birthday: {type: Date, required: true},
    friendRequest: {type: Array},
    friend: {type: Array}
});


UserSchema.pre('save', function(next) {
    var user = this;
    console.log("email exists");
    // only hash the password if it has been modified (or is new)
    if (!user.isModified('password')) return next();
    // generate a salt
    bcrypt.genSalt(SALT_WORK_FACTOR, function(err, salt) {
        if (err) return next(err);
        // hash the password along with our new salt
        bcrypt.hash(user.password, salt, null, function(err, hash) {
            if (err) return next(err);
            // override the cleartext password with the hashed one
            user.password = hash;
            next();
        });
    });    
});

UserSchema.methods.comparePassword = function(candidatePassword, cb) {
    bcrypt.compare(candidatePassword, this.password, function(err, isMatch) {
        if (err) return cb(err);
        cb(null, isMatch);
    });
};


module.exports = mongoose.model('User', UserSchema);
2
  • What does the err parameter contain for an error message? Commented Jul 18, 2013 at 15:33
  • MongoError: Cannot apply $push/$pushAll modifier to non-array Commented Jul 18, 2013 at 15:36

1 Answer 1

1

So the document that mongo finds matching the provided userId does not have an array as its friendRequest property. Look at that specific document by ID in mongo shell and fix it so that friendRequest is an array.

Sign up to request clarification or add additional context in comments.

3 Comments

Sorry can you provide an example, I think I understand but I am still having the problem. What I think I have to do is go in the mongoose UserSchema and change friendRequest: [String], like that or something? But I still get the error, unless I am going about it wrong
It's not a CODE problem. It's a DATA problem with the data in your mongo database. Or, specifically a mismatch between the data your code expects to be there and the data that is actually there. Go look at THAT SPECIFIC RECORD in your database and you'll see friendRequest is not an array.
Thank you sorry, yeah I did not realize the account I was testing with was created before the new array type property was added to the account! Sorry about that.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.