Questions regarding creating/updating a collection using payload local API.

default discord avatar
fabcodingzest2 years ago
1 1

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?
The last way for now I see is installing mongoose and use its way to convert the string into objectID if there is no way provided by the payload (still don't know if that will work but it will add mongoose dependency just for this).

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?

  • Selected Answer
    discord user avatar
    DanRibbens
    2 years ago

    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?).

    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?

    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?

    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.
    If you do want to have separate collections for carts vs orders, then you will have to make hooks that create and update the data through the checkout workflow. Alternatively, you can add your own endpoints to express and use Payload local APIs (or mongoose) to do what you need.

    Does that help? LMK if you have other questions.

    1 reply
  • default discord avatar
    fabcodingzest2 years ago

    Hey Dan,

    Hooks is what you're looking for, either at the field or collection level

    Thanks this made a lot of things clear and I managed to do things with hooks.

    As for having cart and orderItem as one, I decided to separate them as products will be updated over time along with the price so that would create a problem in the order Items data so choosing two different collections, but your idea was nice too thanks, I am gonna use that trick in future XD. ( This is just a practice project but I am trying to make it realistic xD).

    Again thanks for the help.

Star on GitHub

Star

Chat on Discord

Discord

online

Can't find what you're looking for?

Get help straight from the Payload team with an Enterprise License.