簡體   English   中英

如何從Javascript中的數組中取出object中的某個字段值

[英]How to get out a certain field value from a object from a array in Javascript

我正在使用 JavaScript 學習 firebase 雲函數,

我得到一個文檔集合的 QuerySnapshot,每個文檔都有一個 ID 字段和一個消息字段。

我現在遇到的問題是,每次循環遍歷集合時,我都希望能夠從每個 object 中取出 ID 字段並保存。

我已經嘗試了谷歌上出現的所有我能想到的方法,但堆棧溢出對我沒有用,我顯然做錯了什么。

我是 JavaScript 的新手,所以如果有人有任何信息,這可能是一個簡單的解決方法

這是我在 visual studio 中使用的代碼,從我可以看到的地方可以正常工作,可以到達我需要的集合


// onDelete is my trigger which would then go and fetch the collection that I want 

exports.onDelet = functions.firestore.document('recentMsg/currentuid/touid/{documentId}').onDelete(async(snap, context) => {
const data = snap.data();
const contex = context.params;

// once I get the information from onDelete the following code starts

await admin.firestore().collection(`messages/currentuid/${contex.documentId}`)
.get()
  .then((snapshot) => {
    //this array will hold all documents from the collection
      const results = []
    const data = snapshot.docs.map((doc) => ({
         id: doc.id,
      ...doc.data(),
    }));

    results.push(data)
    console.log(results)

     //This is one of the ways I've tried 
    //results.forEach((doc) => { 
   //console.log(doc.id)
  //this is a print out in the terminal
 // >  undefined
// } );

});

下面是我在終端中得到的打印輸出,它包含的所有信息都很棒,但實際上我想要的是擁有一個包含每個 id 的數組,如果數組中只有一個值我知道這不會是個問題,但因為有一個 object 有多個值,所以這就是問題所在。

i  functions: Beginning execution of "onDelet"
>  [
>    [
>      { id: '28ZyROMQkzEBBDwTm6yV', msg: 'sam 2' },
>      { id: 'ixqgYqmwlZJfb5D9h8WV', msg: 'sam 3' },
>      { id: 'lLyNDLLIHKc8hCnV0Cgc', msg: 'sam 1' }
>    ]
>  ]
i  functions: Finished "onDelet" in ~1s

如果這是一個愚蠢的問題,我再次道歉,我是新手。

我正在用 JavaScript 學習 firebase 雲功能,

我得到一個文檔集合的 QuerySnapshot,每個文檔包含一個 ID 字段和一個消息字段。

我現在卡住的地方是,每次我遍歷集合時,我都希望能夠從每個 object 中取出 ID 字段並保存它。

我已經嘗試了所有我能想到的在谷歌上出現的方法,堆棧溢出對我沒有用,我顯然做錯了什么。

我對 JavaScript 完全陌生,所以如果有人有任何信息,這可能很容易解決

這是我正在使用的視覺工作室中的代碼,從我可以看到的地方可以正常工作以獲取我需要的集合


// onDelete is my trigger which would then go and fetch the collection that I want 

exports.onDelet = functions.firestore.document('recentMsg/currentuid/touid/{documentId}').onDelete(async(snap, context) => {
const data = snap.data();
const contex = context.params;

// once I get the information from onDelete the following code starts

await admin.firestore().collection(`messages/currentuid/${contex.documentId}`)
.get()
  .then((snapshot) => {
    //this array will hold all documents from the collection
      const results = []
    const data = snapshot.docs.map((doc) => ({
         id: doc.id,
      ...doc.data(),
    }));

    results.push(data)
    console.log(results)

     //This is one of the ways I've tried 
    //results.forEach((doc) => { 
   //console.log(doc.id)
  //this is a print out in the terminal
 // >  undefined
// } );

});

下面是我在終端中得到的打印輸出,它包含了它所擁有的所有信息,這很棒,但我真正想要的是有一個數組來保存每個 id,如果數組中只有一個值我知道這不會是一個問題,但因為有一個 object 有多個值,這就是問題所在。

i  functions: Beginning execution of "onDelet"
>  [
>    [
>      { id: '28ZyROMQkzEBBDwTm6yV', msg: 'sam 2' },
>      { id: 'ixqgYqmwlZJfb5D9h8WV', msg: 'sam 3' },
>      { id: 'lLyNDLLIHKc8hCnV0Cgc', msg: 'sam 1' }
>    ]
>  ]
i  functions: Finished "onDelet" in ~1s

如果這是一個愚蠢的問題,我再次道歉,我是新手。

使用await admin.firestore().collection( messages/currentuid/${contex.documentId} ).get()你會得到一個支持forEach而不是QuerySnapshotmap 只需像這樣編寫代碼:

// onDelete is my trigger which would then go and fetch the collection that I want 

exports.onDelet = functions.firestore.document('recentMsg/currentuid/touid/{documentId}').onDelete(async(snap, context) => {
const data = snap.data();
const contex = context.params;

// once I get the information from onDelete the following code starts

await admin.firestore().collection(`messages/currentuid/${contex.documentId}`)
.get()
  .then((snapshot) => {
    //this array will hold all documents from the collection
      const results = []
    
    snapshot.forEach((doc) =>{
      results.push({id:doc.id,...doc.data()})
    });

    console.log(results)

     //This is one of the ways I've tried 
    //results.forEach((doc) => { 
   //console.log(doc.id)
  //this is a print out in the terminal
 // >  undefined
// } );

});

我假設messages/currentuid/${contex.documentId}是一個集合而不是單個文檔。

您可以在此處閱讀更多內容。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM