如何将此JSON文件导入Firestore数据库?

时间:2019-02-02 20:58:40

标签: reactjs firebase google-cloud-firestore

我设置使用谷歌火力地堡的认证过程。我现在正在开始使用Firestore数据库-因为我需要使用多个集合-并且在需要导入的JSON文件中包含大量数据。

JSON文件如下所示。我将只发布前两个条目,但总共有60个条目,只是我自己键入的内容不太可行。

如果有人可以指导我逐步解决该问题的脚本使用方法,将不胜感激。谢谢!

我试图利用Stack上的其他一些帖子,但是正是脚本的使用使我全都陷入了困境。

[
 {
   "FirstName": "Nickeil",
   "LastName": "Alexander-Williams",
   "Position": 2,
   "Age": 19,
   "CollegeYear": 2,
   "Height": 77,
   "DisplayHeight": "6-5",
   "Weight": 205,
   "College": "Virginia Tech",
   "Consistency": 46,
   "Greed": 72,
   "Loyalty": 58,
   "PlayForWinner": 16,
   "PlayingTime": 26,
   "Personality": 53,
   "Durability": 47,
   "WorkEthic": 75,
   "DunkRate": 56,
   "RARate": 3,
   "DriveKick": 58,
   "DriveShot": 0,
   "PostUp": 0,
   "PullUp": 17,
   "CS": 32,
   "Pass": 68,
   "LocATB": 30,
   "LocCorner": 81,
   "LocMidrange": 27,
   "LocPaint": 42,
   "FG_RA": 58,
   "FG_ITP": 58,
   "FG_MID": 31,
   "FG_COR": 18,
   "FG_ATB": 67,
   "FT": 0,
   "Scoring": 35,
   "Passing": 40,
   "Handling": 41,
   "OReb": 51,
   "DReb": 61,
   "Block": 52,
   "Steal": 23,
   "DrawFoul": 5,
   "Defender": 54,
   "Discipline": 75,
   "BballIQ": 65,
   "FG_RA_POT": 65,
   "FG_ITP_POT": 65,
   "FG_MID_POT": 38,
   "FG_COR_POT": 25,
   "FG_ATB_POT": 74,
   "FT_POT": 7,
   "Scoring_POT": 42,
   "Passing_POT": 47,
   "Handling_POT": 48,
   "OReb_POT": 58,
   "DReb_POT": 68,
   "Block_POT": 59,
   "Steal_POT": 30,
   "DrawFoul_POT": 12,
   "Defender_POT": 61,
   "Discipline_POT": 82,
   "BballIQ_POT": 72
 },
 {
   "FirstName": "RJ",
   "LastName": "Barrett",
   "Position": 2,
   "Age": 19,
   "CollegeYear": 1,
   "Height": 78,
   "DisplayHeight": "6-6",
   "Weight": 210,
   "College": "Duke",
   "Consistency": 61,
   "Greed": 83,
   "Loyalty": 77,
   "PlayForWinner": 80,
   "PlayingTime": 81,
   "Personality": 76,
   "Durability": 44,
   "WorkEthic": 79,
   "DunkRate": 80,
   "RARate": 17,
   "DriveKick": 51,
   "DriveShot": 28,
   "PostUp": 20,
   "PullUp": 40,
   "CS": 45,
   "Pass": 35,
   "LocATB": 40,
   "LocCorner": 84,
   "LocMidrange": 35,
   "LocPaint": 54,
   "FG_RA": 54,
   "FG_ITP": 54,
   "FG_MID": 39,
   "FG_COR": 26,
   "FG_ATB": 66,
   "FT": 2,
   "Scoring": 40,
   "Passing": 27,
   "Handling": 36,
   "OReb": 57,
   "DReb": 65,
   "Block": 37,
   "Steal": 39,
   "DrawFoul": 13,
   "Defender": 61,
   "Discipline": 54,
   "BballIQ": 58,
   "FG_RA_POT": 61,
   "FG_ITP_POT": 61,
   "FG_MID_POT": 46,
   "FG_COR_POT": 33,
   "FG_ATB_POT": 73,
   "FT_POT": 9,
   "Scoring_POT": 47,
   "Passing_POT": 34,
   "Handling_POT": 43,
   "OReb_POT": 64,
   "DReb_POT": 72,
   "Block_POT": 44,
   "Steal_POT": 46,
   "DrawFoul_POT": 20,
   "Defender_POT": 68,
   "Discipline_POT": 61,
   "BballIQ_POT": 65
 },
 .
 .
 .
]

1 个答案:

答案 0 :(得分:1)

我在编写代码时遇到了同样的问题。对于将JSON导入到Firebase数据库中,没有特定的方法,但是您可以通过脚本将其插入。为此,我编写了此脚本。这可能对您也有帮助。

const fcm = require('nodefire-realtime');

//For Initialize Firebase DB
let serviceAccountKey = require("./serviceAccountKey.json");
let dbUrl = "https://<Your-project-id>.firebaseio.com/";
fcm.init(dbUrl, serviceAccountKey);

const data = require('./<your-JSON-file-name>.json');

//For insert
let dbReference = 'data/user/';
let data =data;
fcm.insert(data, dbReference);

只需创建一个简单的JS文件,例如app.js 并粘贴上面的代码。您可以从项目设置(firebase)中获取 serviceAccountKey.json 。 并且,如果您对此程序包感到困惑,请按照nodefire-realtime中的步骤进行操作。我希望这会有所帮助。