1

ユーザーIDを取得し、それを私のマングースファイルのfriendRequestフィールドにスローするfriendRequestsの正しいデータを取得しています。$push を追加してルート ファイルの friendRequest 配列にデータを追加すると、実際にはデータが挿入されず、作成した err 関数が返されます。

ここに私のルートファイルがあります:

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

                }

            });
};

マングースファイルは次のとおりです。

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);
4

1 に答える 1

1

そのため、提供された userId に一致する mongo が検出したドキュメントには、friendRequestプロパティとして配列がありません。その特定のドキュメントを mongo シェルで ID で確認しfriendRequest、配列になるように修正します。

于 2013-07-18T15:37:54.907 に答える