Algolia partialUpdateObject不工作在Express应用程序(React前端)



EDITED添加带有错误信息的编辑代码。

我是Algolia的新手,我遇到了partialUpdateObject函数的问题。我的应用程序使用Prisma数据库,一切都如预期的那样更新。我也在前端没有问题,这是反应如果这是有帮助的。我的后台是Express。我提前为这组代码块道歉。我已经尝试了这么多的变化,我没有主意了。我还逐字逐句地尝试了Algolia提供的示例代码。同时,我能同步Algolia当我创建一个新的记录。

在我的Algolia仪表板中,每个记录包含一个Algolia分配的objectID。当我console.log我的对象时,该对象返回为未定义。

我的代码很长,因为它包含了更新Prisma的代码,所以我将首先添加带有Prisma的代码,然后是Algolia代码,然后是错误消息。我非常感谢任何帮助!我99%确定我的函数的其余部分也没有正确的结构(仍然熟悉async和promises)。

With Prisma Update


const updateSandbox = async (req, res) => {
const { id } = req.params;
const {
firstName,
lastName,
email,
street,
city,
state,
zip,
phone,
interests,
} = req.body;
const sandbox = await prisma.sandbox.update({
where: {
id,
},
data: {
firstName,
lastName,
email,
street,
city,
state,
zip,
phone,
interests,
},
});
res.status(200).json({ sandbox });
const api_sandbox = {
firstName: sandbox.firstName,
lastName: sandbox.lastName,
email: sandbox.email,
street: sandbox.street,
city: sandbox.city,
state: sandbox.state,
zip: sandbox.zip,
phone: sandbox.phone,
interests: sandbox.interests,
id: sandbox.id,
objectID: sandbox.objectID,
};
index
.partialUpdateObject(api_sandbox, {
createIfNotExists: true,
})
.catch((err) => {
console.log(err);
});
};

仅限Algolia代码

```
const api_sandbox = {
firstName: sandbox.firstName,
lastName: sandbox.lastName,
email: sandbox.email,
street: sandbox.street,
city: sandbox.city,
state: sandbox.state,
zip: sandbox.zip,
phone: sandbox.phone,
interests: sandbox.interests,
id: sandbox.id,
objectID: sandbox.objectID,
};

index
.partialUpdateObject(api_sandbox, {
createIfNotExists: true,
})
.catch((err) => {
console.log(err);
});
```

错误消息

{
name: 'ApiError',
message: "The 'objectID' attribute is required to use action=partialUpdateObject near line:1 column:270",
status: 400,
transporterStackTrace: [
{
request: [Object],
response: [Object],
host: [Object],
triesLeft: 3
}
]
}

再次感谢您的帮助。我还在学习和试验,所以谢谢你的耐心。

在这篇文章的评论的帮助下,我终于弄明白了!我不确定为什么objectID是未定义的,但我没有使用Algolia的自动生成的objectID。我分配了沙盒。id作为objectID。效果很好。我在Algolia的网站上注意到,他们不建议使用自动生成的目标——也许这就是原因。以下是我的代码供参考(只是Algolia部分):

const api_sandbox = {
firstName: sandbox.firstName,
lastName: sandbox.lastName,
email: sandbox.email,
street: sandbox.street,
city: sandbox.city,
state: sandbox.state,
zip: sandbox.zip,
phone: sandbox.phone,
interests: sandbox.interests,
objectID: sandbox.id,
};
index
.partialUpdateObject(api_sandbox, {
createIfNotExists: true,
}).catch((err) => {
console.log(err);
});
console.log(api_sandbox)