How to import CSV or JSON to firebase cloud firestore

前端 未结 11 995
星月不相逢
星月不相逢 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 17:15

    1 - Importing only collections

    If the names of your collections are not only composed of numbers, then you can define the name of the document as follows.

    
        ...
        ...
    
        } else {
            // Collection's length of is always odd.
            for (const key in data) {
              // // Resolve each collection.
    
              // If key is a number it means that it is not a collection
              // The variable id in this case will be the name of the document field or you 
              // can generate randomly
              let id = !isNaN(key) ? data[key].id : key;
    
              await resolve(data[key], [...path, id]);
            }
          }
        }
    
    

    2 - Import collections and sub-collections

    In the same way as in the example above, the name of the sub-collection can not contain only numbers.

        ...
        ...
    
        for (const key in data) {
          // Resolve each collection and remove it from document data.
          if (isCollection(data[key], [...path, key])) {
            // Remove a collection from the document data.
            delete documentData[key];
    
            // If key is a number it means that it is not a collection
            // The variable id in this case will be the name of the document field or you 
            // can generate randomly
            let id = !isNaN(key) ? data[key].id : key;
    
            // Resolve a colleciton.
            resolve(data[key], [...path, id]);
          }
        }
    
        ...
        ...
    

    Note.: Changes in @Maciej Caputa's code

提交回复
热议问题