Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/445.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/apache-kafka/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 如何仅从Firestore中的文档发送特定于Algolia的字段_Javascript_Firebase_Google Cloud Firestore_Algolia - Fatal编程技术网

Javascript 如何仅从Firestore中的文档发送特定于Algolia的字段

Javascript 如何仅从Firestore中的文档发送特定于Algolia的字段,javascript,firebase,google-cloud-firestore,algolia,Javascript,Firebase,Google Cloud Firestore,Algolia,我不熟悉使用Algolia。我正在尝试从Firestore向Aloglia发送用户数据,以便稍后在我创建的应用程序中搜索这些数据。我已经成功地设置了一切,我的所有功能都通过Firebase云功能工作。因此,当我在Firestore中为“用户”创建文档时,它会将所有字段传递给Algolia,更新和删除这些数据也会在Algolia中体现出来 但是,为了维护安全性,我不想将所有用户数据发送到Algolia,而是只发送几个字段。其中包括“displayName”和“username”(用户集合中的两个字

我不熟悉使用Algolia。我正在尝试从Firestore向Aloglia发送用户数据,以便稍后在我创建的应用程序中搜索这些数据。我已经成功地设置了一切,我的所有功能都通过Firebase云功能工作。因此,当我在Firestore中为“用户”创建文档时,它会将所有字段传递给Algolia,更新和删除这些数据也会在Algolia中体现出来

但是,为了维护安全性,我不想将所有用户数据发送到Algolia,而是只发送几个字段。其中包括“displayName”和“username”(用户集合中的两个字段)

所以我的问题是,我如何修改我的代码,只将这两个字段发送到Algolia? 请提供删除和更新的答案

我的代码:

const functions=require(“firebase函数”);
const admin=require('firebase-admin');
常量algoliasearch=require('algoliasearch');
const ALGOLIA_APP_ID=“我的钥匙”;
const ALGOLIA_ADMIN_KEY=“我的钥匙”;
const ALGOLIA_INDEX_NAME=“users”;
var client=algoliasearch(ALGOLIA_APP_ID,ALGOLIA_ADMIN_KEY);
admin.initializeApp(functions.config().firebase);
exports.createUser=functions.firestore
.document('users/{userID}')
.onCreate(异步(快照、上下文)=>{
const newValue=snap.data();
newValue.objectID=snap.id;
var index=client.initIndex(ALGOLIA_index_NAME);
index.saveObject(newValue);
});
exports.updateUser=functions.firestore
.document('users/{userID}')
.onUpdate(异步(快照、上下文)=>{
const afterUpdate=snap.after.data();
afterUpdate.objectID=snap.after.id;
var index=client.initIndex(ALGOLIA_index_NAME);
index.saveObject(更新后);
})
exports.deleteUser=functions.firestore
.document('users/{userID}/'))
.onDelete(异步(快照、上下文)=>{
const oldID=snap.id;
var index=client.initIndex(ALGOLIA_index_NAME);
index.deleteObject(oldID);
});Algolia介绍了如何对索引执行添加、更新和删除操作。请注意,要添加和更新,您需要传递
objectID

只添加某些字段 使用示例中的一些代码,下面是如何仅将Firestore对象的某些字段传递给Algolia():

仅更新某些字段 还受到以下因素的启发:

删除 尽管我没有这样做,但还是可以这样做的,而且文档表明您应该使用
deleteObject
方法,因为它性能更高。你可以做:

exports.deleteUser = functions.firestore
    .document("users/{userId}")
    .onDelete((snap) => {
        return index.deleteObject(snap.id);
    });
exports.updateUser = functions.firestore
    .document("users/{userId}")
    .onUpdate(async (snap, context) => {
        const afterUpdate = snap.after.data();
        const updateUser = {
            objectID: context.params.userId,
            displayName: afterUpdate.displayName,
            username: afterUpdate.username,
        };

        await index.partialUpdateObject(updateUser);
    });
exports.deleteUser = functions.firestore
    .document("users/{userId}")
    .onDelete((snap) => {
        return index.deleteObject(snap.id);
    });