我正在使用 Firebase 函式為全文搜索應用程式創建 Algolia 索引
當有人在 Firebase 上發帖時,我是這樣創建索引的:
export const onListingCreated = functions.firestore
.document('Listings/{listingId}')
.onCreate((snap, ctx) => {
return index.saveObject({
objectID: snap.id,
...snap.data(),
})
})
//...snap.data() has - AdTitle:"House" Email:"[email protected]"
//this works well the index is created like this on algolia
但我想避免共享電子郵件
我試過這樣:
export const onListingCreated = functions.firestore
.document('Listings/{listingId}')
.onCreate((snap, ctx) => {
return index.saveObject({
objectID: snap.id,
...snap.data().AdTitle,
})
})
//on algolia the index was created like:
//0:H
//1:o
//2:u
//3:s
//4:e
I wanted to be AdTitle: "House"
有沒有辦法避免在 algolia 上共享敏感資訊?
uj5u.com熱心網友回復:
在您的情況下snap.data().AdTitle是字串House,并且您正在擴展字串,這使您的輸出物件看起來像那樣。一旦你傳播了一個字串,它就會被轉換為一個陣列。例如在javascriptHouse中轉換為["H", "o", "u", "s", "e"]Now陣列也是物件的型別,所以當你嘗試{...["H", "o", "u", "s", "e"]}它時,它會["H", "o", "u", "s", "e"]作為{ 0: "H", 1: "o", 2: "u", 3: "s", 4: "e" }并傳播到那個物件上,給出你提到的內容
let randomString = 'House'
let obj = {...randomString} //converts to array and then spreads the array
console.log(obj)
let arr = [...randomString]
console.log(arr)
let obj2 = {...arr}
console.log(obj2) //obj and obj2 give same result
相反,您可以做的是
let randomString = 'House'
let obj = {AdTitle: randomString}
console.log(obj)
或者您可以從資料物件中洗掉該Email屬性,然后使用您通常的實作
.onCreate((snap, ctx) => {
const dataObj = snap.data()
delete dataObj.Email
return index.saveObject({
objectID: snap.id,
...dataObj,
})
})
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/479888.html
