I tested the code on MongoDB Playground and it works. But when I take it to the route.ts it gives me the error.
On Playground working fine:
db.getCollection('users').findOneAndUpdate({
"email": "email@gmail.com",
"boxes.name":"kjhkjh",
}
,{
$set: {"boxes.$[e1].pokemon.$[e2].isChecked": true
}}
,{new:true,
arrayFilters: [
{"e1.name": "kjhkjh"},
{"e2.customBoxId": "0215_01-1748780059793"}
]
}
)
Not working on route.ts with variable:
export async function PUT(request: Request ,{params}: Params) {
//
const email = (await params).email
const boxName = (await params).boxName
const customBoxId = (await params).customBoxId
const {newCheck} = await request.json()
try {
await Users.findOneAndUpdate(
{"email": email, "boxes.name": boxName},
{
$set: {"boxes.$[e1].pokemon.$[e2].isChecked": newCheck}
},
{new: true,
arrayFilters: [
{"e1.name": boxName},
{"e2.customBoxId": customBoxId}
]}
)
return NextResponse.json({success: true, message:"Record deleted succesfully"})
} catch (error) {
return NextResponse.json({success: false, message: `No data found. Error: ${error}`})
}
}
Error showing:
Error: Could not find path "boxes.0.pokemon.0.customBoxId" in schema
at async PUT (...\api\users\[email]\[boxName]\[customBoxId]\route.ts:33:8)
31 | // ]}
32 | // )
> 33 | await Users.findOneAndUpdate({
| ^
34 | "email": email,
35 | "boxes.name":boxName,
36 | }
PUT /api/users/email@gmail.com/kjhkjh/0199_01-1748780059793 200 in 1745ms
Thanks for helping