Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/444.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 Firebase云函数_Javascript_Firebase_Google Cloud Firestore_Google Cloud Functions - Fatal编程技术网

Javascript Firebase云函数

Javascript Firebase云函数,javascript,firebase,google-cloud-firestore,google-cloud-functions,Javascript,Firebase,Google Cloud Firestore,Google Cloud Functions,我正在使用Firebase cloud函数从另一个文档创建新文档 基本上,我在一个名为reps{}的文档中有一个字段,它以userId作为键,int作为值 我想检查reps{}中值的和是否大于100(示例) 我有一个onUpdate函数,它工作得很好,但是我需要添加这个特性。我试过这个: var count = 0; admin.firestore() .collection("posts") .doc(userId) .collection("userPosts") .doc(

我正在使用Firebase cloud函数从另一个文档创建新文档

基本上,我在一个名为reps{}的文档中有一个字段,它以userId作为键,int作为值

我想检查reps{}
值的和是否大于100(示例)

我有一个onUpdate函数,它工作得很好,但是我需要添加这个特性。我试过这个:

var count = 0;
admin.firestore()
  .collection("posts")
  .doc(userId)
  .collection("userPosts")
  .doc(postId).get().then(doc =>
  { 
    doc['reps'].values.forEach(val =>
    { 
      count += val;
     });
  });

  console.log(count);
通过这个查询,我得到了reps映射,我如何计算映射中所有值的总和

admin
  .firestore()
  .collection("posts")
  .doc(userId)
  .collection("userPosts")
  .doc(postId).get().then(function (doc)
  {
    if (doc.exists)
    {
      console.log(doc.get("reps"));
    }
  });
通过做

admin.firestore()
  .collection("posts")
  .doc(userId)
  .collection("userPosts")
  .doc(postId).get()
您查询的是一个文档,该方法返回一个承诺,该承诺将以

因此,

doc['reps'].values
这是行不通的

您需要使用DocumentSnapshot的方法,如下所示:

admin.firestore()
  .collection("posts")
  .doc(userId)
  .collection("userPosts")
  .doc(postId).get().then(doc =>
  { 
    var respObj = doc.get('reps');
    Object.entries(respObj).forEach(([key, value]) => { 
      count += value;
     });
  });
通过做

admin.firestore()
  .collection("posts")
  .doc(userId)
  .collection("userPosts")
  .doc(postId).get()
您查询的是一个文档,该方法返回一个承诺,该承诺将以

因此,

doc['reps'].values
这是行不通的

您需要使用DocumentSnapshot的方法,如下所示:

admin.firestore()
  .collection("posts")
  .doc(userId)
  .collection("userPosts")
  .doc(postId).get().then(doc =>
  { 
    var respObj = doc.get('reps');
    Object.entries(respObj).forEach(([key, value]) => { 
      count += value;
     });
  });

所以我就想出来了:

我使用get()获取我要查找的字段

要计算值之和,我使用了:

Object.keys(rep).forEach(函数(值)
{
var值=代表[值];
repCount+=值;
});

这是我的密码:

await admin
  .firestore()
  .collection("posts")
  .doc(userId)
  .collection("userPosts")
  .doc(postId).get().then(function (doc)
  {
    if (doc.exists)
    {
      var rep = doc.get("reps");
      Object.keys(rep).forEach(function (values)
      {
        var value = rep[values];
        repCount += value;
      });
    }
  });

所以我就想出来了:

我使用get()获取我要查找的字段

要计算值之和,我使用了:

Object.keys(rep).forEach(函数(值)
{
var值=代表[值];
repCount+=值;
});

这是我的密码:

await admin
  .firestore()
  .collection("posts")
  .doc(userId)
  .collection("userPosts")
  .doc(postId).get().then(function (doc)
  {
    if (doc.exists)
    {
      var rep = doc.get("reps");
      Object.keys(rep).forEach(function (values)
      {
        var value = rep[values];
        repCount += value;
      });
    }
  });

我认为您不能直接从上述书面查询中获取文档。看到这个:@UmarHussain,谢谢你的回复。那么,我应该如何编写我的查询呢?我想您不能直接从上面的书面查询中获取文档。看到这个:@UmarHussain,谢谢你的回复。那我该怎么写我的查询呢?是的,我刚想出来。谢谢你的回复。我尝试了你的代码,在firebase控制台中我得到了这个错误:TypeError:doc.get(…)。forEach不是一个函数是的,请参阅更新,我的代码中有一个错误!是的,我刚想出来。谢谢你的回复。我尝试了你的代码,在firebase控制台中我得到了这个错误:TypeError:doc.get(…)。forEach不是一个函数是的,请参阅更新,我的代码中有一个错误!