Closed CoreyFedde closed 7 years ago
It seems to be close to these errors: https://stackoverflow.com/questions/32457569/mongowriteconcernexception-the-immutable-field-id-was-found-to-have-been-a https://stackoverflow.com/questions/31775150/node-js-mongodb-the-immutable-field-id-was-found-to-have-been-altered
But so far the answers haven't be applicable
Frontend
const updateUpload = function (data) {
console.log('data.file.id is: ', data.file.id)
console.log('data is: ', data)
console.log('data.file is: ', data.file)
return $.ajax({
url: config.apiOrigin + '/uploads/' + data.file.id,
method: 'PATCH',
headers: {
Authorization: 'Token token=' + store.user.token
},
data
})
}
const openUpdateModal = function () {
const fileName = $(this).attr('data-name')
const fileDes = $(this).attr('data-des')
const fileTag = $(this).attr('data-tag')
const fileId = $(this).attr('data-id')
$('#fileName').val(fileName)
$('#fileDes').val(fileDes)
$('#fileTag').val(fileTag)
$('#fileId').val(fileId)
// $('#updateSubmit').data(id, fileId)
}
const onUpdateUpload = function (event) {
const data = getFormFields(event.target)
event.preventDefault()
uploadApi.updateUpload(data)
.then(uploadUi.succes)
.catch(uploadUi.failure)
}
Backend
// Updates JSON object
const update = (req, res, next) => {
delete req.body._owner // disallow owner reassignment.
req.upload.update(req.body.upload)
.then(() => res.sendStatus(204))
.catch(next)
}
const update = (req, res, next) => {
delete req.body._owner // disallow owner reassignment.
// gets specific about what is being set within the req.body.upload and does not change the _id
Upload.update({_id: req.body.upload_id}, { $set: {name: req.body.name, description: req.body.description, tag: req.body.tag} })
.then((data) => console.log(data))
.then(() => res.sendStatus(204))
.catch(next)
}
We have updated the code and no longer get an error, but the changes are not being saved.
I believe you need to pass a function as the third argument for this syntax to work properly, something like:
(err,upload) => {
if(err) {
console.log(`Error in setting upload: ${err}`);
res.send(err);
} else {
console.log('Success updating upload', upload)
res.send(upload)
}
}
The new version is under branch feature-tags
this is the old update function code for the controller:
const update = (req, res, next) => {
delete req.body._owner // disallow owner reassignment.
req.upload.update(req.body.upload)
.then(() => res.sendStatus(204))
.catch(next)
}
@CoreyFedde can you link me to your repo/branch?
@payne-chris-r Hey Chris, my repo/branch is here: https://github.com/The-Sleeping-Samurais/sleeping-samurai-back/tree/feature-tags
curl script?
ID="5988e63c6a4aaca88360ff40"
TOKEN="WR04rRGtHqOFSE0Bcf1lAF3Up+w6HgrudR1UQ/pJvuo=--QvUCWmCNLOscVpUIS+k6mhytttMt4VxvFEX8Gttn+78="
TEXT="hello"
URL="www.zipcar.com"
TAG="fun"
DESCRIPTION="Short blurb"
API="http://localhost:4741"
URL_PATH="/uploads"
curl "${API}${URL_PATH}/${ID}" \
--include \
--request PATCH \
--header "Content-Type: application/json" \
--header "Authorization: Token token=${TOKEN}" \
--data '{
"upload": {
"name": "'"${TEXT}"'",
"url": "'"${URL}"'",
"tag": "'"${TAG}"'",
"description": "'"${DESCRIPTION}"'"
}
}'
echo
This was the curl script I used to test - the ID and Token were specific to the Upload I created and me
@CoreyFedde Any updates on this?
We got it to work!
The problems we identified:
Here is our solution: In our API, we went back to the original update code that mirrors the example included in the express template. WRONG:
const update = (req, res, next) => {
delete req.body._owner // disallow owner reassignment.
Upload.update({_id: req.body.upload_id}, { $set: {name: req.body.name, description: req.body.description, tag: req.body.tag} }, (err, upload) => {
if (err) {
console.log(`Error in setting upload: ${err}`)
res.send(err)
} else {
console.log('Success updating upload', upload)
return upload
}
})
.then((upload) => upload.save())
req.upload.update(req.body.upload)
.then(() => res.sendStatus(204))
.catch(next)
}
RIGHT:
// Updates JSON object
const update = (req, res, next) => {
delete req.body._owner // disallow owner reassignment.
req.upload.update(req.body.upload)
.then(() => res.sendStatus(204))
.catch(next)
}
On our front-end, we changed our naming convention for the getFormFields from file[key] to upload[key]. WRONG:
<form id="edit-upload-form">
<label>File Name</label><br>
<input id="fileName" class="edit-upload" type="text" name="file[name]" >
<br><label>Description</label><br>
<input id="fileDes" class="edit-upload" type="text" name="file[description]" >
<br><label>Tag</label><br>
<input id="fileTag" class="edit-upload" type="text" name="file[tag]" >
<button id="updateSubmit" type="submit" class="btn btn-default" value="edit" name="submit">Edit Upload</button>
</form>
RIGHT:
<form id="edit-upload-form">
<label>File Name</label><br>
<input id="fileName" class="edit-upload" type="text" name="upload[name]" >
<br><label>Description</label><br>
<input id="fileDes" class="edit-upload" type="text" name="upload[description]" >
<br><label>Tag</label><br>
<input id="fileTag" class="edit-upload" type="text" name="upload[tag]" >
<button id="updateSubmit" type="submit" class="btn btn-default" value="edit" name="submit">Edit Upload</button>
</form>
We are encountering an error with our patch requests from the front end. We are updating three fields, file.name, file.description, and file.tag, which are grabbed by getFormFields. We are also passing an ID through the getFormFields.
The data and ID we are passing the request looks good, but we are getting a 500 error message each time.
"After applying the update to the document {_id: ObjectId('598749093669f60848b47dd8') , ...}, the (immutable) field '_id' was found to have been altered to _id: ObjectId('5988be2b3dcde8084c6e5f25')""
The problem seems to be with our mongoDB, but the ObjectId it is returning to us is completely different than the one we are passing it.
We are getting similar errors with our curl requests.
For problem solving, we've console.logged various forms of our data, shifted the data we are passing the request, changed our method for grabbing the ID, and are currently looking through our backend.
Any thoughts?