router.updateQA = (req, res) => {
const data = (req.method == 'POST') ? req.body : req.query;
const id = req.query._id;
const fields = 'name, profile.qa'
UserData.model
.findById(id)
.exec((err, user) => {
if (err) return res.apiError(err);
if (!user) return res.apiResponse({ message: "no user" });
user.getUpdateHandler(req).process(
data,
{
flashErrors: true,
fields: fields,
errorMessage: 'error!!'
},
err => {
if (err) return res.apiError("error", err);
res.apiResponse({
status: "success",
result: user
});
}
);
})
}
https://keystonejs.com/api/list/update-item/
fields: String|Array
A list of fields to attempt to update. All other fields will not be updated even if values are provided.
If a string is passed, it will attempt to use list-to-array to transform the string.
즉 fields 에 주어진 필드들 외에 필드들은 값이 주어지더라도 업데이트 안함. 그리고 주어진 필드중의 필드를 업데이트 하지 않아도 상관없음.
그러나 업데이트 할 Document 의 필드 속성중 Relationship Type 필드들은 업데이트시 값을 주지 않으면 값이 날라감. 그외 필드들은 영향 없음
그래서 RelationshipType이 들어가는 Document 업데이트는 무조건 사용해야할듯
Ex)
-User Model
User.add({
... ,
password: { type: Types.Password, initial: true, required: true },
name: { type: Types.Name },
birthday: { type: Types.Date, index: true, default: Date.now },
gender: { type: Types.Select, options: 'M, W', default: 'M' },
nationality: { type: Types.Relationship, ref: 'Nationality', initial: true },
...
})
let fields = 'password, name, gender, birthday';
if (data.nationality) fields += ', nationality';
password, name, gender, birthday 필드들은 relationship 타입이 아니라 값을 넣어도 되고 안넣어도 됨.
하지만 relationship 타입인 nationality 가 fields 에 들어있고 api 날릴때 key, value 값이 주어지지 않으면 null 값이 들어가 원래 값이 날라감.
그래서 User update 할 때 data 에 nationality 키가 포함 되어있으면 fields 에 포함시키고 아니면 포함 안 시킴.