Firebase云功能Firestore数组联合 [英] Firebase cloud function firestore array union

查看:47
本文介绍了Firebase云功能Firestore数组联合的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我无法获得数组联合或增量以在Firebase云函数中正常工作.

I'm unable to get the array union or increment to work properly in firebase cloud functions.

return docRef.update({

  object: {
    count: admin.firestore.FieldValue.increment(1),
    list: admin.firestore.FieldValue.arrayUnion({
      space_id: newData.date_id,
      user: {
        displayName: "john doe"
      }
    })
  }

该函数运行时,它只会覆盖列表数组中的现有数据,并且即使当前存在且为数字类型,计数也始终设置为1.

When the function runs, it simply overwrites the existing data in the list array and the count is always set to 1 even though it currently exists and is of number type.

推荐答案

在您发表评论之后,下面是我尝试过的HTML代码. 请注意,不是Cloud Function代码(使用Admin SDK),而是某些JavaScript代码使用JavaScript SDK.,但很可能Admin SDK具有相同的行为.

Following you comment, here is below the HTML code I tried. Note that it is not Cloud Function code (which uses the Admin SDK) but some JavaScript code using the JavaScript SDK. But most probably the Admin SDK has the same behavior.

要尝试执行以下操作:

  1. 在Firestore中创建一个集合testSO,并创建一个ID为doc1和一个虚拟字段的文档.
  2. 将此HTML页面保存在您的计算机上,然后在浏览器中将其打开.
  3. 然后更改值并在浏览器中重新加载页面以进行实验
  1. Create a collection testSO in Firestore and a doc with ID doc1 and one dummy field.
  2. Save this HTML page on your computer and open it in browser.
  3. Then change the values and reload the page in the browser for experimenting the behavior

当与arrayUnion(字段array1)的字符串数组和increment(字段count1)的数字数组一起使用时,您会看到arrayUnionincrement都可以工作,但不能与复合对象.

You will see that both arrayUnion and increment work when used with an array of strings for arrayUnion (field array1) and a number for increment (field count1) but not with the compounded object.

<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <title>Title</title>

    <script src="https://www.gstatic.com/firebasejs/5.9.3/firebase-app.js"></script>
    <script src="https://www.gstatic.com/firebasejs/5.9.3/firebase-database.js"></script>
    <script src="https://www.gstatic.com/firebasejs/5.9.3/firebase-auth.js"></script>
    <script src="https://www.gstatic.com/firebasejs/5.9.3/firebase-functions.js"></script>
    <script src="https://www.gstatic.com/firebasejs/5.9.3/firebase-firestore.js"></script>
      </head>

  <body>
    <script>
      // Initialize Firebase
      var config = {
        apiKey: 'xxxxxxxxxxx',
        authDomain: 'xxxxxxxxxxx',
        databaseURL: 'xxxxxxxxxxx',
        projectId: 'xxxxxxxxxxx'
      };

      firebase.initializeApp(config);

      var db = firebase.firestore();

      db.doc('testSO/doc1').update({
        count1: firebase.firestore.FieldValue.increment(1),
        array1: firebase.firestore.FieldValue.arrayUnion('arrayItem'),
        object: {
          count: firebase.firestore.FieldValue.increment(1),
          list: firebase.firestore.FieldValue.arrayUnion({
            space_id: 'spaceNbr',
            user: {
              displayName: 'john doe'
            }
          })
        }
      });
    </script>
  </body>
</html>

这篇关于Firebase云功能Firestore数组联合的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆