Javascript 无法从Algolia中删除对象
我有一个post对象,最初是有人创建的,然后这将触发一个Firebase函数,然后将其添加到Algolia以便稍后查询。当用户删除帖子时,从Algolia中删除对象时,我遇到了一些问题。该功能在Firebase中被触发,但它正在“更新”帖子,而不是将其删除 以下是Firebase功能控制台的日志:Javascript 无法从Algolia中删除对象,javascript,firebase,firebase-realtime-database,google-cloud-functions,algolia,Javascript,Firebase,Firebase Realtime Database,Google Cloud Functions,Algolia,我有一个post对象,最初是有人创建的,然后这将触发一个Firebase函数,然后将其添加到Algolia以便稍后查询。当用户删除帖子时,从Algolia中删除对象时,我遇到了一些问题。该功能在Firebase中被触发,但它正在“更新”帖子,而不是将其删除 以下是Firebase功能控制台的日志: onPostWrite: Function execution took 79 ms, finished with status: 'ok' onPostWrite: Function executi
onPostWrite: Function execution took 79 ms, finished with status: 'ok'
onPostWrite: Function execution started
onPostWrite: Updated post -Lb3lcmWn1iXAdnbFDmo
以下是为函数执行的代码:
exports.onPostWrite = functions.database.ref('/posts/{postId}').onWrite((snapshot, context) => {
const index = client.initIndex(ALGOLIA_INDEX_POSTS);
//When the data is first created...
if (snapshot.before.exists()) {
const post = snapshot.before.val();
post.objectID = context.params.postId;
console.log('Updated post ', post.objectID);
return index.saveObject(post);
}
//When data has been deleted...
if (!snapshot.after.exists()) {
const post = snapshot.before.val();
post.objectID = context.params.postId;
console.log('Deleted post ', post.objectID);
return index.deleteObject(post.objectID);
}
if (!snapshot.before.exists()) {
const post = snapshot.after.val();
post.objectID = context.params.postId;
console.log('Created post ', post.objectID);
return index.saveObject(post);
}
});
正如您所知,它只执行“Update”命令,而不是create或delete命令
我想这可能与我的IF语句有关,但我不确定。如果有任何帮助,我们将不胜感激。您需要切换
if
语句的顺序
// Called onUpdate, a snapshot exists before the update and exists after the update
// Called onDeletion, a snapshot exists before the update and doesn't exist after the update
if (snapshot.before.exists()) {
// Called onDeletion, nothing exists after operation
if (!snapshot.after.exists()) {
// Called onCreate. Nothing existed `before`, but it does now
if (!snapshot.before.exists()) {
因为您在每个块中返回,所以即使删除第一个块,执行也会在第一个块之后停止。我想如果你交换前两个街区,它会像预期的那样工作:
// Called onDelete, nothing exists after operation
if (!snapshot.after.exists()) {
// Called onUpdate, a snapshot exists before the update and exists after the update
// Called onDelete, a snapshot exists before the update and doesn't exist after the update
// Since you've moved a "better" `onDelete` above this, this function will now only be called for onUpdate
if (snapshot.before.exists()) {
// Called onCreate. Nothing existed `before`, but it does now
if (!snapshot.before.exists()) {
注意:您实际上不需要最后一条if
语句,因为前两条语句将捕获delete
和update
操作,最后一条是代码可以采用的唯一其他路径。以下是我对这一切的看法:
exports.onPostWrite = functions
.database
.ref('/posts/{postId}')
.onWrite((snapshot, context) => {
const index = client.initIndex(ALGOLIA_INDEX_POSTS);
// no snapshot.before.exists(), post has been created
if (!snapshot.before.exists()) {
const post = snapshot.after.val();
post.objectID = context.params.postId;
console.log('Creating post ', post.objectID);
return index.saveObject(post);
}
const post = snapshot.before.val();
post.objectID = context.params.postId;
// no snapshot.after.exists(), post has been deleted
if (!snapshot.after.exists()) {
console.log('Deleting post ', post.objectID);
return index.deleteObject(post.objectID);
}
// snapshot.before.exists() AND snapshot.after.exists(), post has been updated
console.log('Updating post ', post.objectID);
return index.saveObject(post);
});
你能记录
index.deleteObject(post.objectID)吗代码>?现在您正在记录删除的帖子
,除了ID本身之外,它不会给您任何有用的遥测信息。另外,您使用哪种类型的API键来初始化索引?我无法记录这部分代码,因为函数从未达到该点。当我进入我的应用程序并删除帖子时,该功能的代码不会被触发。这就是我的问题。我也在使用Algolia admin API密钥。在这种情况下,问题似乎出在上!snapshot.after.exists()
我通过为创建、更新和删除部分创建三个单独的函数,实际上修复了整个问题。然而,这有点低效,我想找出这里出了什么问题。我来玩玩!snapshot.after.exists()
并查看其是否有效。是的,如果您找到答案,请告诉我们!