ga-wdi-boston / capstone-project

Other
3 stars 29 forks source link

Issue with Updating Resources (Mongo/Mongoose) #844

Closed kailinc closed 6 years ago

kailinc commented 6 years ago

I am sending an AJAX request for updating resource and I keep getting the same error back. The error message is

 {"error":{"message":"After applying the update to the document {_id: ObjectId('59a1adb45f8229648451d081') , ...}, the (immutable) field '_id' was found to have been altered to _id: ObjectId('59a61ed44b40408661e5451c')","error":{"name":"MongoError","message":"After applying the update to the document {_id: ObjectId('59a1adb45f8229648451d081') , ...}, the (immutable) field '_id' was found to have been altered to _id: ObjectId('59a61ed44b40408661e5451c')","driver":true,"index":0,"code":66,"errmsg":"After applying the update to the document {_id: ObjectId('59a1adb45f8229648451d081') , ...}, the (immutable) field '_id' was found to have been altered to _id: ObjectId('59a61ed44b40408661e5451c')"}}

When I do a curl script to update and delete, I don't get any problem.

kailinc commented 6 years ago

Plus: It is a 500 Internal Service error, so I am positive that my request is formatted correctly

:4741/carts/59a61ed44b40408661e5451c:1 PATCH http://localhost:4741/carts/59a61ed44b40408661e5451c 500 (Internal Server Error)
payne-chris-r commented 6 years ago

Can you do a log of the req (make it an echo server) or make your Ajax requests fire to httpbin.org so you can see what they actually look like. My guess is you're setting ID unintentionally, and Express is trying to update it, and then Mongo barfs.

kailinc commented 6 years ago

This is my curl script. Basically products is an array of objects. It seems like Mongo or Mongoose creates a new doc for each object within the array. Within each object I want to pass in a new quantity. I also tried having products with an array without an item object

TOKEN='neyA8l8vc7Ab3opPW2csjLJe8s53psyc6ZY9vq7zqRs=--AZB1KuSktHmv4wcP17S97YHK2j3wFn0qKnqyq7L5Fdo='
ID='59a61ed44b40408661e5451c'
API="http://localhost:4741"
URL_PATH="/carts"

curl "${API}${URL_PATH}/${ID}" \
  --include \
  --request PATCH \
  --header "Content-Type: application/json" \
  --header "Authorization: Token token=${TOKEN}" \
  --data '{
    "cart": {
      "products": [{"item": "59a197c79f399e9a7f9725af", "quantity": "1", "name": "javaScript", "img": "https://s3.us-east-2.amazonaws.com/mango-bytes/languages-frameworks/js.png", "devCred": "100", "basePrice": "5", "attributes": "javaScript+2" }, {"item": "59a197c79f399e9a7f9725b0", "quantity": "1", "name": "python", "img": "https://s3.us-east-2.amazonaws.com/mango-bytes/languages-frameworks/python.png", "devCred": "100", "basePrice": "5", "attributes": "python+2" }]
    }
  }'
kailinc commented 6 years ago

Yeah! Let me do that. How do I send the request to httpbin.org? I set url to be url: 'httpbin.org' but it sends to localhost:/httpbin.org

payne-chris-r commented 6 years ago

We do it as the first step in upload-client (as part of the multer talk). That's the only way I've done it. I'd have to look at the httpbin docs. Happy to do that with you if your'e not sure how to navigate them though!

kailinc commented 6 years ago

Hey Chris. It would be great if you could help me with the httpbin docs after lunch. I'll get started with the docs and look for mongoose bugs. I also want to talk to you about project scope.

kailinc commented 6 years ago

It seems like the data is passed in as form according to https.bin

kailinc commented 6 years ago

This is from httpbin

{args: {…}, data: "", files: {…}, form: {…}, headers: {…}, …}
args
:
__proto__
:
Object
data
:
""
files
:
{}
form
:
{"cart":{"products":[{"attributes":"python 2","basePrice":"5","devCred":"100","img":"https://s3.us-east-2.amazonaws.com/mango-bytes/languages-frameworks/python.png","name":"python","quantity":1,"item":"59a197c79f399e9a7f9725b0","id":"59a6dab44a53061849cf8b2d"}]}}
:
""
__proto__
:
Object
headers
:
{Accept: "*/*", Accept-Encoding: "gzip, deflate", Accept-Language: "en-US,en;q=0.8", Authorization: "Token token=vyeGwJcunA9oJ8LKqyhdRNkXOYcS8T7GwTrtYo…xc=--T9ZmOz7huijxQ0zsXH8hQnUnRK3b59onz2X8SdcveHw=", Cache-Control: "no-cache", …}
json
:
null
origin
:
"144.121.82.194"
url
:
"http://httpbin.org/patch"
__proto__
:
Object
kailinc commented 6 years ago

I looked at my ajax request to make sure I have the right format. This seems correct, I am not sure what is happening.

const update = function (data, id) {
  return $.ajax({
    method: 'PATCH',
    url: 'http://httpbin.org/patch',
    headers: {
      Authorization: 'Token token=' + store.user.token
    },
    data
  })
}
kailinc commented 6 years ago

I also notice there is an id for products which shouldn't be there. I am going to filter it out and try that.

scottyscripts commented 6 years ago

try that. also can you post what data looks like that you are passing into update()

kailinc commented 6 years ago

I tried filtering out the id, and i still get the same problem.

data passing into update

{"cart":{"products":[{"attributes":"python+2","basePrice":"5","devCred":"100","img":"https://s3.us-east-2.amazonaws.com/mango-bytes/languages-frameworks/python.png","name":"python","quantity":1,"item":"59a197c79f399e9a7f9725b0"}]}}
kailinc commented 6 years ago

I tried sending ajax request to httpbin.org to see if data is in form or data. Data is under form, so it is in the right place. I am not sure what is going on.

{args: {…}, data: "", files: {…}, form: {…}, headers: {…}, …}
args
:
{}
data
:
""
files
:
{}
form
:
credentials[coderName]
:
"1"
credentials[email]
:
"1@1"
credentials[firstName]
:
"1"
credentials[lastName]
:
"1"
credentials[password]
:
"1"
__proto__
:
Object
headers
:
{Accept: "*/*", Accept-Encoding: "gzip, deflate", Accept-Language: "en-US,en;q=0.8", Cache-Control: "no-cache", Connection: "close", …}
json
:
null
origin
:
"144.121.82.194"
url
:
"http://httpbin.org/post"
__proto__
:
Object
kailinc commented 6 years ago

Hey chris I am getting the same error after trying the ajax requests.

kailinc commented 6 years ago

error message:

After applying the update to the document {_id: ObjectId('59a1adb45f8229648451d081') , ...}, the (immutable) field '_id' was found to have been altered to _id: ObjectId('59a7032dd01d9528b9ca20fd')
payne-chris-r commented 6 years ago

Do you know which is which? Is it somehow trying to update the cart to the item's id??

kailinc commented 6 years ago

im just gonna let this sit. im gonna work on adding item to cart, so i can meet requirements.

payne-chris-r commented 6 years ago

Isn't that what we were doing?

kailinc commented 6 years ago

The current status is that now I can update with new items in the cart. I haven't tried updating carts with items removed or quantity changed. The problem might be that i did JSON.stringify and putting that in ajax request. I didn't do JSON.stringify for updating cart with new items. I will revisit this later