如何在Javascript中从数组中从对象中获取特定字段值

如何在Javascript中从数组中从对象中获取特定字段值,javascript,google-cloud-firestore,google-cloud-functions,Javascript,Google Cloud Firestore,Google Cloud Functions,我正在学习使用JavaScript的firebase云函数 我得到一个文档集合的QuerySnapshot,每个文档包含一个ID字段和一个消息字段 我现在陷入困境的地方是,每次我在集合中循环时,我都希望能够从每个对象中取出ID字段并保存它 我已经尝试了所有我能想到的在谷歌上出现的方法,堆栈溢出没有一个对我有效,我显然做错了什么 我对JavaScript完全陌生,所以如果有人有任何信息,这可能是一个简单的修复方法 这是我在VisualStudio中使用的代码,从我可以看到的地方到我需要的集合,它运

我正在学习使用JavaScript的firebase云函数

我得到一个文档集合的QuerySnapshot,每个文档包含一个ID字段和一个消息字段

我现在陷入困境的地方是,每次我在集合中循环时,我都希望能够从每个对象中取出ID字段并保存它

我已经尝试了所有我能想到的在谷歌上出现的方法,堆栈溢出没有一个对我有效,我显然做错了什么

我对JavaScript完全陌生,所以如果有人有任何信息,这可能是一个简单的修复方法

这是我在VisualStudio中使用的代码,从我可以看到的地方到我需要的集合,它运行良好


// 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的数组 如果数组中只有一个值,我知道这不会有问题,但因为存在一个具有多个值的对象,这就是问题所在

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


再次道歉,如果这是一个愚蠢的问题,我是一个新手。

你可以试试这样的问题

const data = snapshot.docs.map((doc) => ({
    id: doc.id,
    ...doc.data(),
}));
data.forEach((doc)=> {
    if(doc.id === 'ixqgYqmwlZJfb5D9h8WV'){
        console.log(doc.id);
    }
})

使用
wait admin.firestore().collection(
messages/currentuid/${contex.documentId}
).get()
可以获得一个
QuerySnapshot
,它支持
forEach
而不是
映射。只需像这样编写代码:

//onDelete是我的触发器,然后它将去获取我想要的集合
exports.onDelet=functions.firestore.document('recentMsg/currentuid/touid/{documentId}')。onDelete(异步(快照,上下文)=>{
const data=snap.data();
const contex=context.params;
//一旦我从onDelete获得了信息,下面的代码就开始了
等待admin.firestore()集合(`messages/currentuid/${contex.documentId}`)
.get()
。然后((快照)=>{
//此数组将保存集合中的所有文档
常量结果=[]
snapshot.forEach((doc)=>{
push({id:doc.id,…doc.data()})
});
console.log(结果)
//这是我尝试过的方法之一
//results.forEach((doc)=>{
//console.log(doc.id)
//这是终端中的打印输出
//>未定义
// } );
});
我假设
messages/currentuid/${contex.documentId}
是一个集合,而不是一个单独的文档


你可以阅读更多信息。

想知道这个
const data=snapshot.docs.map((doc)=>({id:doc.id}));
@Umer Malik,
results.push(data)
是不必要的,
数据
是数据的数组,注释和您注释的代码段应该工作您的数据返回数组。
映射()
method创建一个新的数组,并填充结果。啊,好的,谢谢你提供的信息是的,这是一个集合,谢谢你的帮助,在我使用你的代码后,我明白了你的意思