我正在尝试使用 Contentful 中的 patch 方法填充一个空字段。以下代码在一个克隆环境中可以工作,但在另一个克隆环境中则不起作用。
let patchData: OpPatch[] = [
{
op: 'replace',
path: '/fields/keywords',
value: entryKeyword,
},
];
await cmaClient.entry.patch({ entryId: entryId }, patchData, { 'X-Contentful-Version': entryVersion });
当我尝试执行此操作时,收到“无法处理的实体”错误:
UnprocessableEntity: {
"status": 422,
"statusText": "Unprocessable Entity",
"message": "Could not apply patch to entry: invalid patch",
"details": {},
"request": {
"url": "/spaces/xyz/environments/abc/entries/123456789",
"headers": {
"Accept": "application/json, text/plain, */*",
"Content-Type": "application/json-patch+json",
"X-Contentful-User-Agent": "sdk contentful-management-plain.js/7.54.2;",
"Authorization": "Bearer ...",
"user-agent": "node.js/v14.19.2",
"Accept-Encoding": "gzip",
"X-Contentful-Version": 25,
"Content-Length": 78
},
"method": "patch",
"payloadData": "[{\"op\":\"replace\",\"path\":\"/fields/keywords\",\"value\":\"test keyword\"}]"
},
"requestId": "abcd-123456"
}
我对这两个环境具有完全相同的访问权限。我错过了什么?
我遇到了同样的问题 - 结果当条目没有您要修补的文件时 - 它会抛出如上所述的错误。
OpPatch = {
op: DOES_FIELD_ALREADY_EXIST ? 'replace' : 'add',
path: '/fields/keywords',
value: entryKeyword,
};
主要问题是Contentful需要存在字段才能更改它。如果还没有字段,那么我们必须“添加”它而不是替换任何内容。