在 NodeJS 中,我使用的是 "@hubspot/api-client": "^7.1.2"。
使用 accessToken 创建 hubspot 客户端,如下
const hubSpotClient = new hubspot.Client({ accessToken });
当我尝试使用电子邮件更新联系人时,它抛出错误
请求:
const idProperty = 'email';
const response = await hubSpotClient(store).crm.contacts.basicApi.update(email, idProperty, contact);
回应:
ERROR {
"statusCode": 404,
"body": {
"status": "error",
"message": "Object not found. objectId are usually numeric.",
"correlationId": "71e911d3...",
"context": {
"id": [
"testemail@..."
]
},
"category": "OBJECT_NOT_FOUND"
}
创建联系人与该客户工作正常,但通过电子邮件更新不起作用。 传递 idProperty 时有任何不合适的地方或语法错误吗?
最佳答案
问题出在您的实现中,因为您似乎没有正确使用 Hubspot API。
如果您check the function signature basicApi.update
public async update(contactId: string, simplePublicObjectInput: SimplePublicObjectInput, idProperty?: string, _options?: Configuration): Promise<RequestContext> {
基本上,您需要传递一个 contactId,然后传递一个 simplePublicObjectInput
is basically an object这代表您的更新。
您的代码应如下所示:
import { Client } from "@hubspot/api-client";
const hubspotClient = new Client({ accessToken: YOUR_ACCESS_TOKEN });
const contactID = 1234;
const response = await hubspotClient.crm.contacts.basicApi.update(contactID, {
properties: { email: '<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="acc1d581c2c9db81c9c1cdc5c0eccbc1cdc5c082cfc3c1" rel="noreferrer noopener nofollow">[email protected]</a>' },
})
请记住,Hubspot 始终尝试遵循与其端点相同的准则。如果您检查the endpoint specification您将看到以下内容:
将 Hubspot Node 客户端视为某些 http 客户端的抽象,但最终的作用与实现中描述的端点完全相同。
因此,在您的实现中,Hubspot 返回一个适当的错误,因为您没有在第一个参数中提供 contactId,Hubspot 会告诉您:“找不到对象。objectId 通常是数字。”因为确实联系人 ID 是数字,而您正在使用电子邮件 --string-- 的值。
如果您想“通过电子邮件更新”
我认为没有直接的方法可以做到这一点,您可能需要通过电子邮件预先搜索联系人。
您可以使用 searchApi
。
获取 ID 后,只需运行更新即可。
const searchResponse = await hubspotClient.crm.contacts.searchApi.doSearch({
filterGroups: [
{
filters: [
{
value: '<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="56333b373f3a7b22397b25333724353e16313b373f3a7835393b" rel="noreferrer noopener nofollow">[email protected]</a>',
propertyName: 'email',
operator: 'EQ',
},
],
},
],
sorts: [],
properties: [],
limit: 1,
after: 0,
});
// Off course you need to improve a lot the error handling here and so on.
// This is just an example
const [contactID] = searchResponse.results;
const contactUpdateResponse = await hubspotClient.crm.contacts.basicApi.update(contactID, {
properties: { email: '<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="3c51451152594b1159515d55507c5b515d5550125f5351" rel="noreferrer noopener nofollow">[email protected]</a>' },
})
希望这对您有帮助!
关于javascript - HubspotClient - 通过电子邮件 ID 更新联系人不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/73606034/