第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

Firebase Cloud Firestore doc.data().includes

Firebase Cloud Firestore doc.data().includes

狐的傳說 2022-12-18 15:41:02
我正在嘗試使用一個數(shù)組,嘗試在我存儲在云 firestore 上的數(shù)組上運行doc.data().includes,doc.data().indexOf該數(shù)組是文檔中唯一存在的東西。數(shù)據(jù)是這樣排列的    0: example@gmail.com    1: firebase@gmail.com    2: new@gmail.com為了獲取數(shù)據(jù),我只是這樣做firebase.firestore().collection('mails').doc('emails').get().then((doc) => {            if (doc.exists) {                console.log(doc.data()); //prints the list of emails in form of array                doc.data().IndexOf(email); //error IndexOf doesn't exist                doc.data().includes(email); //error includesOf doesn't exist            }            else {                console.log('data doesn't exist');            }        });
查看完整描述

2 回答

?
四季花海

TA貢獻1811條經(jīng)驗 獲得超5個贊

要訪問doc.data().IndexOfordoc.data().includes或任何其他與數(shù)組相關(guān)的函數(shù),只需doc.data()像這樣轉(zhuǎn)換為數(shù)組:


var docArray = Object.values(doc.data());

docArray.includes(stringToSearchFor);

要么


Object.values(doc.data()).includes(stringToSearchFor);


這將允許您運行docArray.includes和docArray.IndexOf。轉(zhuǎn)換的原因是 firebase 返回整個文檔,它是對象的對象,您可以通過 console.log(doc.data()) 和原型字段看到這一點。發(fā)生這種情況是因為 firestore 將數(shù)組存儲為一個 json 對象,這意味著您看到的數(shù)組索引號實際上并不是索引號,它們只是對象鍵。


意義


0: example@gmail.com

1: firebase@gmail.com

2: new@gmail.com

0, 1, 2索引(或看起來)在哪里并且example@gmail.com, firebase@gmail.com, new@gmail.com是值,但在 firestore 中0, 1, 2實際上只是鍵,它們不是索引。因此,為了獲得我們剛剛運行的鍵(數(shù)字)或值(電子郵件)Object.keys(doc.data())// will return numbers,Object.values(doc.data())//will return emails您將獲得所需的數(shù)組,現(xiàn)在您可以對該數(shù)組運行操作。


查看完整回答
反對 回復(fù) 2022-12-18
?
心有法竹

TA貢獻1866條經(jīng)驗 獲得超5個贊

我建議您將數(shù)據(jù)存儲在數(shù)組中,而不是像 Firestore 數(shù)據(jù)庫屏幕截圖所示的單獨字段。


您的 Firestore 文檔當(dāng)前包含 6 個不同的字段,并且doc.data()您將此文檔中的所有 6 個字段作為一個 JavaScript 對象檢索:這就是 Array 方法無法工作的原因。如果將這些數(shù)據(jù)存儲在一個數(shù)組中(即一個數(shù)組類型的字段)就可以了。


您可以嘗試以下代碼:


  const docRef = firebase.firestore().collection('mails').doc('emails');


  docRef

    .set({

      mails: ['example@gmail.com', 'firebase@gmail.com', 'new@gmail.com'],

    })

    .then(() => {

      return docRef.get();

    })

    .then((doc) => {

      if (doc.exists) {

        const email = 'example@gmail.com';

        console.log(doc.data().mails); 

        console.log(doc.data().mails.indexOf(email)); 

        console.log(doc.data().mails.includes(email)); 

      }

    });


查看完整回答
反對 回復(fù) 2022-12-18
  • 2 回答
  • 0 關(guān)注
  • 155 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動學(xué)習(xí)伙伴

公眾號

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號