Unable to Change attribute in javascript json object

Posted in :

透過 resetful api 的 ajax 傳回來的 json object 無法寫入值, 替代解法很多, 有人是轉成object, 建議可以這樣做:

 let tmpObj = JSON.parse(JSON.stringify(myApiVar));

資料來源:
https://stackoverflow.com/questions/70873021/unable-to-change-attribute-in-js-object-passed-from-parent-as-api-variable-in-li

Try to .toObject() the form:
https://stackoverflow.com/questions/30879898/unable-to-add-property-to-the-json-object

Form.findOneAndUpdate(condition, req.body, {upsert:true}, function(err, form){

    if (err) return res.send(500, { error: err });

    var objForm = form.toObject();

    objForm.status = "saved successfully";

    return res.send(objForm);

});

發佈留言

發佈留言必須填寫的電子郵件地址不會公開。 必填欄位標示為 *