Simplify your stack and build anything. Or everything.
Build tomorrow’s web with a modern solution you truly own.
Code-based nature means you can build on top of it to power anything.
It’s time to take back your content infrastructure.

payload.update() error

default discord avatar
castletech2 years ago
16

When using payload.update() like this:



const updatedProduct = await payload.update({


collection: 'products',


id: '6596d373eb4df61dcdeb978a',


data: {name: 'New Product Name'},


})



I get the following error:


unhandledRejection TypeError: Cannot read properties of undefined (reading 'id')



The id is a real productID. Did I miss something? payload.find works fine with that exact same id

  • default discord avatar
    tyteen4a032 years ago

    If it's a real ID it should work, do you have more code you can share / context?

  • default discord avatar
    castletech2 years ago

    Sure!:



    const orderId = searchParams.orderId


    const { docs: orders } = await payload.find({


    collection: 'orders',


    depth: 2,


    where: {


    id: {


    equals: orderId,


    },


    },


    })



    const [order] = orders


    const products = order.products as Product[]


    const sizes = order.sizes ? (order.sizes as { size: string, id: string }[]).map(size => size.size) : [];



    async function updateProducts() {


    for (let i = 0; i < products.length; i++) {


    let product = products[i];


    let size = sizes[i];


    console.log(product.id)


    try {


    const updatedProduct = await payload.update({


    collection: 'products',


    where: {


    // required


    id: { equals: '6596d373eb4df61dcdeb978a' },


    },


    data: {name: 'New Product Name'},


    })


    console.log(updatedProduct)


    } catch (error) {


    console.error(' Error when updating the product:', error)


    }


    }


    }



    The console.log(product.id) is the exact same id (6596d373eb4df61dcdeb978a)

  • default discord avatar
    tyteen4a032 years ago

    Just to check you have done

    payload.init()

    before interacting with payload right?



    or is this a custom endpoint?

  • default discord avatar
    castletech2 years ago

    A custom endpoint I think





    This is the whole file including the payload.find for the order which is working perfectly fine, just the update isnt working because of that error

  • default discord avatar
    tyteen4a032 years ago

    This looks like a RSC?

  • default discord avatar
    castletech2 years ago

    This is the database entry for that Product



    Its basically a page to display the order details to the user after the user has completed an order. The user then gets redirected to that page when stripe confirms the payment

  • default discord avatar
    tyteen4a032 years ago

    I mean, is this supposed to be a react server component

  • default discord avatar
    castletech2 years ago

    Yea I guess 😅

  • default discord avatar
    tyteen4a032 years ago

    I looked at the code again, you may have forgotten to

    await

    updateProducts()

  • default discord avatar
    castletech2 years ago

    Sadly this wasn't the solution

  • default discord avatar
    lioloc2 years ago

    Random guess but is it perhaps because its not recognizing the format that you pass the id as? Maybe it shouldn't be a string and instead you should use

    equals: product.id

    directly?

  • default discord avatar
    castletech2 years ago

    I have already tried that, still get the same error

  • default discord avatar
    tyteen4a032 years ago

    I wonder if you're hitting a next-payload bug



    do you have the full stacktrace?

  • default discord avatar
    lioloc2 years ago

    Do you mine sharing how you resolved this?

    @543715670999695360
  • default discord avatar
    castletech2 years ago

    The issue was with the hook which I defined in my Product collection, I edited my hook and the problem was solved

  • default discord avatar
    Deleted User2 years ago

    Do you have a full working exampl you can share with us? I'm also running into payload.update issues

Star on GitHub

Star

Chat on Discord

Discord

online

Can't find what you're looking for?

Get dedicated engineering support directly from the Payload team.