How to add multiple map objects (JSON's) to cloud-firestore database?

wapn

I've a function that set one JSON to my Cloud Firestore database:

app.post('/api/add/collection/:collection_id/permission/:permission_id', (req, res) => {
(async () => {
    try {
        await db.collection(req.params.collection_id)
            .doc(req.params.permission_id)
            .set({
                [req.body.id]: {
                    name: req.body.name,
                    email: req.body.email,
                    phone_number: req.body.phone_number
                }
            }, { merge: true}
            );

        return res.status(200).send('OK');
    } catch (error) {
        console.log(error);
        return res.status(500).send('ERROR' + error);
    }
})();
});

To invoke this function, I pass JSON body to POST request:

https://.../app/api/add/collection/USER_ID/permission/PERMISSION_id

{
    "id": 45,
    "name": "Stack",
    "email": "[email protected]",
    "phone_number": "+48 111 222 333"
}

Everything work fine, this request set this JSON as a map with id as a key, and values as a name, email and phone_number. But I want to pass several JSON's on one request to add several map objects to my NoSQL document, like:

[
    {
        "id": 45,
        "name": "Stack",
        "email": "[email protected]",
        "phone_number": "+48 111 222 333"
    },
    {
        "id": 46,
        "name": "Stack2",
        "email": "[email protected]",
        "phone_number": "+48 222 222 333"
    },
    {
        "id": 47,
        "name": "Stack3",
        "email": "[email protected]",
        "phone_number": "+48 333 222 333"
    }
]

How to modify my firebase function to achieve this?

Frank van Puffelen

Since you have an array of objects, you'll need to loop over that array, and then create a document for each object. And since you need to wait until all documents are created, you can use Promise.all().

Something like this:

try {
    await Promise.all(req.body.map((object) => {
        return db.collection(req.params.collection_id)
          .doc(req.params.permission_id)
          .set({
            [object.id]: {
                name: object.name,
                email: object.email,
                phone_number: object.phone_number
            }
          }, { merge: true});
    });
    return res.status(200).send('OK');
} catch (error) {
    console.log(error);
    return res.status(500).send('ERROR' + error);
}

この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。

侵害の場合は、連絡してください[email protected]

編集
0

コメントを追加

0

関連記事

分類Dev

How to add/remove from a firestore map field?

分類Dev

Cloud Firestore or Realtime database

分類Dev

How can i import JSON into Cloud Firestore

分類Dev

Add dynamic objects to Firestore

分類Dev

How to retrieve a list of custom objects from Cloud Firestore in Android

分類Dev

How add group of objects to database with incremented id?

分類Dev

How to add map inside document in firestore using flutter?

分類Dev

How to deserialize multiple JSON objects in C#?

分類Dev

How to save loop data in multiple JSON objects

分類Dev

How to add json data in mysql database

分類Dev

How to use Firebase Cloud FireStore Database in a Next JS project. How to initialize it correctly?

分類Dev

Cloud Firestore: Add/remove items in ArrayValue field via REST API's PATCH method?

分類Dev

How do I read and write multiple json objects from 1 S3 file to dynamodb python 3.8

分類Dev

Django database: how to filter objects based on other object's field?

分類Dev

How can I add a FirebaseUser profile update to a batched write with Cloud Firestore?

分類Dev

Add same class to multiple Objects

分類Dev

How to set multiple origins in the CORS json file with Google Cloud Storage?

分類Dev

How to parse multiple nested JSON objects/arrays with GSON?

分類Dev

Using .map() to add incrementing values to JS objects

分類Dev

Flutter Cloud Firestore Map <String、dynamic>エラー

分類Dev

Typescript - Map over a collection to add a property to it to change the type of objects within. How?

分類Dev

While using firestore (firebase cloud database) getting repetitive data

分類Dev

How can I add a google map info panel to this code with an sql database

分類Dev

Cloud Firestore: how to reference another document?

分類Dev

How to access Cloud Firestore from Google Sheets?

分類Dev

How to access Cloud Firestore from Google Sheets?

分類Dev

How can I add multiple className's to react component?

分類Dev

How to get an Array of Objects from Firestore in Swift?

分類Dev

How to update the propriety of a field objects in a firestore document?

Related 関連記事

  1. 1

    How to add/remove from a firestore map field?

  2. 2

    Cloud Firestore or Realtime database

  3. 3

    How can i import JSON into Cloud Firestore

  4. 4

    Add dynamic objects to Firestore

  5. 5

    How to retrieve a list of custom objects from Cloud Firestore in Android

  6. 6

    How add group of objects to database with incremented id?

  7. 7

    How to add map inside document in firestore using flutter?

  8. 8

    How to deserialize multiple JSON objects in C#?

  9. 9

    How to save loop data in multiple JSON objects

  10. 10

    How to add json data in mysql database

  11. 11

    How to use Firebase Cloud FireStore Database in a Next JS project. How to initialize it correctly?

  12. 12

    Cloud Firestore: Add/remove items in ArrayValue field via REST API's PATCH method?

  13. 13

    How do I read and write multiple json objects from 1 S3 file to dynamodb python 3.8

  14. 14

    Django database: how to filter objects based on other object's field?

  15. 15

    How can I add a FirebaseUser profile update to a batched write with Cloud Firestore?

  16. 16

    Add same class to multiple Objects

  17. 17

    How to set multiple origins in the CORS json file with Google Cloud Storage?

  18. 18

    How to parse multiple nested JSON objects/arrays with GSON?

  19. 19

    Using .map() to add incrementing values to JS objects

  20. 20

    Flutter Cloud Firestore Map <String、dynamic>エラー

  21. 21

    Typescript - Map over a collection to add a property to it to change the type of objects within. How?

  22. 22

    While using firestore (firebase cloud database) getting repetitive data

  23. 23

    How can I add a google map info panel to this code with an sql database

  24. 24

    Cloud Firestore: how to reference another document?

  25. 25

    How to access Cloud Firestore from Google Sheets?

  26. 26

    How to access Cloud Firestore from Google Sheets?

  27. 27

    How can I add multiple className's to react component?

  28. 28

    How to get an Array of Objects from Firestore in Swift?

  29. 29

    How to update the propriety of a field objects in a firestore document?

ホットタグ

アーカイブ