0

Hi there I'm trying to make a post request where I want to update one field on sanity.io

this is my query

patch: {
          id: "f6c46b53-9313-4354-a4d6-7a40b06ee4c0",
          set: {
            `rewardItem[_key == \"${key}\"].lastTimeReward`: "TEst",
          },
        }

but this won't let me even run my project, its giving me this error on console.log: Unexpected token;

When I do my query like this, it works

patch: {
          id: "f6c46b53-9313-4354-a4d6-7a40b06ee4c0",
          set: {
            "rewardItem[_key == \"e88959e43ce7\"].lastTimeReward": "Test",
          },
        }
      }]

Thanks a lot.

1 Answers1

1

Your set-property is an object, and you can't enter a dynamic key directly into the object. To do what you are trying to do here, you can wrap the dynamic key in square brackets like this. That should give you the output you desire

const variable = "example"
const a = { [`template ${variable}`]: "value" }
console.log(a)
Sebastian
  • 1,321
  • 9
  • 21