How to import CSV or JSON to firebase cloud firestore

前端 未结 11 1053
星月不相逢
星月不相逢 2020-11-29 16:19

Is there a way to import CSV or JSON to firebase cloud firestore like in firebase realtime database?

11条回答
  •  谎友^
    谎友^ (楼主)
    2020-11-29 16:57

    I used the General Solution provided by Maciej Caputa. Thank you (:

    Here are a few hints. Assuming that you have an Ionic Firebase application installed with the required Firebase node modules in the functions folder inside that solution. This is a standard Ionic Firebase install. I created an import folder to hold the script and data at the same level.

    Folder Hierarchy

    myIonicApp
        functions
            node_modules
                firebase-admin
    ImportFolder
        script.js
        FirebaseIonicTest-a1b2c3d4e5.json
        fileToImport.json
    

    Script Parameters

    const admin = require('../myIonicApp/functions/node_modules/firebase-admin'); //path to firebase-admin module
    const serviceAccount = require("./FirebaseTest-xxxxxxxxxx.json"); //service account key file
    
    admin.initializeApp({
      credential: admin.credential.cert(serviceAccount),
      databaseURL: "https://fir-test-xxxxxx.firebaseio.com" //Your domain from the hosting tab
    });
    

    Creating the Service Account Key File

    • In the Firebase console for your project, next to the Project Overwiew item, click on the gear icon and select Users and permissions
    • At the bottom of the screen, click Advanced permission settings

    • This opens another tab for the Google Cloud Platform Console
    • On the left select the Service Accounts item
    • Create a Service Account for an existing Service Account

    I simply added a key to the App Engine default service account

    The Create key function will offer to download the key to a JSON file

    JSON Data Structure

    To use the script provided, the data structure must be as follows:

    {
      "myCollection" : {
        "UniqueKey1" : {
          "field1" : "foo",
          "field2" : "bar"
        },{
        "UniqueKey2" : {
          "field1" : "fog",
          "field2" : "buzz"
        }...
    }
    

提交回复
热议问题