Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
587 views
in Technique[技术] by (71.8m points)

node.js - Mongoose save() not updating value in an array in database document

I am trying to update a document in a collection (units) using GUI and after it gets updated I want to update the value (user.Units which is an array of Unit names) in collection (users). If the array length is just 1 element it gets updated and also shows up in database and everything works well, but when Array of Units have more than one element , I try to update it through a for loop, it shows it gets updated but when I check the database it is still not updated.

I really can't figure out why its not updating the database when I update the value through a loop.

Whole Edit and update function:-

 edit_unit: function (req, res, next) {
    var Data = req.body;

    Client_data.Unit.findById(req.params.unitId, function (err, unit) {
        var error = false;
        if (err) {
            error = err;
        } else if (!unit) {
            error = "FATAL: unable to look up Unit #" + req.params.unitId;
        } else {

            switch(req.body.name) {
                case 'Icon':
                    var Icon = unit.Icon;

                    User.find({"Units":Icon}, function (err, users) {
                        if (err)
                        console.log(err);

                        users.forEach(function (u) {
                            if (u.Units.length > 1) {
                            for (var i = 0; i <= u.Units.length; i++) {
                               if(u.Units[i] == Icon) {
                                   u.Units[i] = req.body.value;
                               }
                            }
                            }
                            else {
                                u.Units = req.body.value;
                            }
                            u.save(u);
                        });
                    });
                    unit[req.body.name] = req.body.value;
                    break;
                case 'Description':
                    unit[req.body.name] = req.body.value;
                    break;
                default:
                    unit[req.body.name] = req.body.value;
                    break;
            }
            var data = JSON.stringify(req.body);
            unit.save();

            res.writeHead(200, {
                'Content-Length': data.length,
                'Content-Type':  'application/json'
            });
            res.end(data);
        }
    });
}

req.body:-

{ name: 'Icon',
  value: 'Health Utility 22c',
  pk: '5395ed107cd92dc40eaafb56' 
}

User Schema:-

var userSchema = mongoose.Schema({
UserName:     { type: String, required: true },
Password:     { type: String },
FirstName:    { type: String, required: true },
LastName:     { type: String, required: true },
CompanyName:  { type: String },
PhoneNumber:  { type: Number },
StartDate:    { type: Date,   required: true },
EndDate:      { type: Date,   required: true, default: new Date('9999-12-12')  },
ClientID:     { type: ObjectId, ref: 'Client', default: null },
DepartmentID: { type: ObjectId, ref: 'Department' },
ManagerID:    { type: ObjectId, ref: 'User', default: null},
Units:        [ { type: String, required: true } ],
UserList:      { type: Array, default:[]},
Access:    [{ type: String, enum: ['DEMO', 'USER','MANAGER','ADMINISTRATOR','OWNER']}],
Credentials:  { type: String },
AFTE:         { type: Number},
SessionID:    { type: String, default: null }
}, { safe: true });
question from:https://stackoverflow.com/questions/24618584/mongoose-save-not-updating-value-in-an-array-in-database-document

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

Please log in or register to reply this article.

OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...