I want to update a filed according to relations in another field. GET the products works. Then I iterate over products and want to set the field entries.
When I test this in postman with a concrete ID instead of categoryIDs, it workes, but not in my nodejs-script. categoryIDs
is an array of IDs.
[...]
console.log(JSON.stringify({data:{catalog_categories:categoryIDs}})) // => {"data":{"catalog_categories":[1]}}
const dataProducts = await fetch(process.env.STRAPI_URL + '/api/product/' + product.id, {
method: 'PUT',
headers: {
Accept: '*/*',
'Content-Type': 'application/json',
Authorization: `Bearer ${process.env.STRAPI_TOKEN}`
},
body: JSON.stringify({data:{catalog_categories:categoryIDs}})
}).then( responseProducts => responseProducts.json() ).catch(err => console.log(err));
console.log(dataProducts)
This gives a syntax error: Unexpected token M in JSON at position 0
If I change body: JSON.stringify({data:{catalog_categories:categoryIDs}})
to body: {"data":{"catalog_categories":categoryIDs}}
, I get back:
{
data: null,
error: {
status: 400,
name: 'SyntaxError',
message: 'Unexpected token o in JSON at position 1'
}
}
what is ok, because body is not stringified. But why is the stringified version not working?