Questions regarding creating/updating a collection using payload local API. #309
-
Hey! So I am back trying to build this e-commerce project which I am trying for decades(sarcasm), there are problems I am facing with the payload local API methods. I have few situations where I have a relationship field with and without hasMany set to true, now when I want to use payload API to create/update the collection, I pass the id from the data I got, but it throws an error regarding ObjectId. I understand that it is asking for objectId when I am passing the id string (maybe) that I got from data.id (which is supposed to be objectId right?). I wanted to know if there is any way of updating the collection where we have a relationship field (single or as an array) with payload.create/update? If that's not possible then what am I suppose to do in a custom mutation of Checkout I was trying to make where I wanted to clone cartItems to orderItems using payload.create? (cart items fields have couple of relational fields) Oh, and I wanted to somehow delete all items in the cart after mutation is executed but I saw there is only a delete method, not really sure if I can pass an array of ids to id? |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 1 reply
-
Formatting the relationship object can be a little confusing, we added to the documentation to help https://payloadcms.com/docs/fields/relationship#how-the-data-is-saved. Have you seen that yet?
Hooks is what you're looking for, either at the field or collection level. You should not have to use mongoose separately from the rest of your Payload app, in fact this could lead to issues for example Payload hooks or validation that you configure inside of Payload will not run when using mongoose directly. In this discussion #204 RobertAlbus is doing relationship updates using hooks in his project: https://github.com/RobertAlbus/kto-cms/blob/payload-poc/payload-kto/src/collections/hierarchical/HierarchicalHooks.ts Regarding how you model your cart/orders, it is totally up to you, but I have had success modeling an ecommerce system where I set a paymenyID from my processor to an order record and if an order didn't have a that field populated, the system treated it as the cart. I didn't have to worry about deleting the items for a user's cart and adding them to an order, because the data was the same. By sharing one collection for carts and orders you can simplify the workflow for checkout, just an idea. Does that help? LMK if you have other questions. |
Beta Was this translation helpful? Give feedback.
Formatting the relationship object can be a little confusing, we added to the documentation to help https://payloadcms.com/docs/fields/relationship#how-the-data-is-saved. Have you seen that yet?