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
If it's a real ID it should work, do you have more code you can share / context?
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)
Just to check you have done
payload.init()before interacting with payload right?
or is this a custom endpoint?
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
This looks like a RSC?
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
I mean, is this supposed to be a react server component
Yea I guess 😅
I looked at the code again, you may have forgotten to
awaitupdateProducts()
Sadly this wasn't the solution
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.iddirectly?
I have already tried that, still get the same error
I wonder if you're hitting a next-payload bug
do you have the full stacktrace?
Do you mine sharing how you resolved this?
@543715670999695360The issue was with the hook which I defined in my Product collection, I edited my hook and the problem was solved
Do you have a full working exampl you can share with us? I'm also running into payload.update issues
Star
Discord
online
Get dedicated engineering support directly from the Payload team.