ga-wdi-boston / team-project

Other
2 stars 39 forks source link

Trouble patching an array #390

Closed bradleyden closed 7 years ago

bradleyden commented 7 years ago

This is a bit of a continuation from my issue yesterday, but here's my situation:

I am trying to patch the resource "orders" which has a "products" field which is an array, and I'm trying to send a patch request that replaces the products field with a new array, made up of objects.

When I run the request, it says that it is successful, but when I check the orders, the products array in the order I'm trying to patch is the same. I've tried sending the data to Mongoose as both JSON and a JavaScript object, but in both cases I just get a successful response on the request, but the array is not changing.

This is how the data is being formatted:

orders[id]:598b1ad01d321d26437f1bce orders[date_placed]:1969/01/01 orders[isOpen]:true orders[user_id]:59887f5458fa7b1e22d29175 orders[products][0][product_id]:59887fbc58fa7b1e22d29176 orders[products][0][quantity]:4 orders[products][1][product_id]:598888d64167bd24dc38ffad orders[products][1][quantity]:2 orders[products][2][product_id]:5988812358fa7b1e22d29177 orders[products][2][quantity]:1

This is the object I'm trying to patch, after the previous request was sent:

{
"orders": [
{
"_id": "598b1ad01d321d26437f1bce",
"updatedAt": "2017-08-09T14:32:53.084Z",
"createdAt": "2017-08-09T14:23:12.743Z",
"date_placed": "2017-08-07T04:00:00.000Z",
"isOpen": true,
"_owner": "59887f5458fa7b1e22d29175",
"__v": 0,
"products": [
"7"
],
"id": "598b1ad01d321d26437f1bce",
"editable": false
}
]
}

As you can see, the products array does not reflect any kind of change that I am expecting it to.

Finally, here is my schema:

const mongoose = require('mongoose')

const orderSchema = new mongoose.Schema({
  date_placed: {
    type: Date,
    required: true
  },
  products: {
    type: [{}]
  },
  isOpen: {
    type: Boolean,
    required: true
  },
  _owner: {
    type: mongoose.Schema.Types.ObjectId,
    ref: 'User',
    required: true
  }
}, {
  timestamps: true,
  toJSON: {
    virtuals: false,
    transform: function (doc, ret, options) {
      const userId = (options.user && options.user._id) || false
      ret.editable = userId && userId.equals(doc._owner)
      return ret
    }
  }
})

const Order = mongoose.model('Order', orderSchema)

module.exports = Order

I've been trying to get this one patch request to work since yesterday around lunchtime and I'm having no luck at all with figuring out how to deal with complex data types in Mongoose, and I wish I could just use SQL and a join table to approach this problem.

Thanks for any help on this!

jordanallain commented 7 years ago

can you post the relevant controller code?

bradleyden commented 7 years ago
const update = (req, res, next) => {
  delete req.body._owner  // disallow owner reassignment.
  req.order.update(req.body.order)
    .then(() => res.sendStatus(204))
    .catch(next)
}
ryanwk commented 7 years ago

how do you set editable to true? we thought this might be one reason why we can't PATCH looking at the backend we identified this line in the orders controller: ret.editable = userId && userId.equals(doc._owner) this seems to be the only place I could find 'editable'

our object data has this: "editable": false

order controller:

const orderSchema = new mongoose.Schema({
  date_placed: {
    type: Date,
    required: true
  },
  products: {
    type: [[mongoose.Schema.Types.ProductId, Number]]
  },
  isOpen: {
    type: Boolean,
    required: true
  },
  _owner: {
    type: mongoose.Schema.Types.ObjectId,
    ref: 'User',
    required: true
  }
}, {
  timestamps: true,
  toJSON: {
    virtuals: false,
    transform: function (doc, ret, options) {
      const userId = (options.user && options.user._id) || false
      ret.editable = userId && userId.equals(doc._owner)
      return ret
    }
  }
})
bradleyden commented 7 years ago

Update: We also tried sending the data as a POST request instead of a patch, and we are getting a 500 error, so it seems to be that our schema is not accepting the data we are sending it.

We have been reading through documentation and trying everything we can possibly find to adjust the schema to properly read the array data we are sending, but we are completely stalled here because our entire ERD revolves around storing an array of products in an Orders collection. Without being able to send array data in AJAX requests, we can't get any of our basic site functionality to work.

cpearce31 commented 7 years ago

Are you sure that type: [{}] is correct in your schema for products? Have you tried type: [], since you're patching

"products": [
"7"
],

which doesn't involve nesting?

bradleyden commented 7 years ago

Problem solved.

Had our collection name pluralized on the front end when it shouldn't have been and spend about a day over-complicating everything.