将json数组导入Firebase和Firestore时出错

时间:2018-03-03 18:22:23

标签: firebase google-cloud-firestore

我使用此代码将json数组导出到云端防火商店

const admin = require("firebase-admin");
const serviceAccount = require("./service-key.json");
const data = require("./state.json");
admin.initializeApp({
    credential: admin.credential.cert(serviceAccount),
    databaseURL: "https://functionstest-54bd9.firebaseio.com"
});
data && Object.keys(data).forEach(key => {
    const nestedContent = data[key];
    if (typeof nestedContent === "object") {
        Object.keys(nestedContent).forEach(docTitle => {
            admin.firestore()
                .collection(key)
                .doc(docTitle)
                .set(nestedContent[docTitle])
                .then((res) => {
                console.log("Document successfully written!");
            })
                .catch((error) => {
                console.error("Error writing document: ", error);
            });
        });
    }
});

但是当我运行这个时,我收到错误

Error: Argument "data" is not a valid Document. Input is not a plain JavaScript object.
    at Object.exports.(anonymous function) [as isDocument] (/home/me/Documents/TfmFirebase/functions/node_modules/@google-cloud/firestore/src/validate.js:86:15)
    at WriteBatch.set (/home/me/Documents/TfmFirebase/functions/node_modules/@google-cloud/firestore/src/write-batch.js:264:14)
    at DocumentReference.set (/home/me/Documents/TfmFirebase/functions/node_modules/@google-cloud/firestore/src/reference.js:425:8)
    at Object.keys.forEach.docTitle (/home/me/Documents/TfmFirebase/functions/lib/migrate.js:17:18)
    at Array.forEach (<anonymous>)
    at Object.keys.forEach.key (/home/me/Documents/TfmFirebase/functions/lib/migrate.js:13:36)
    at Array.forEach (<anonymous>)
    at Object.<anonymous> (/home/me/Documents/TfmFirebase/functions/lib/migrate.js:10:27)
    at Module._compile (module.js:660:30)
    at Object.Module._extensions..js (module.js:671:10)

我的json文件是

[
    {
        "countryId" : 1,
        "countryName" : "Any",
        "phoneCode" : null,
        "sortName" : null
    },
    {
        "countryId" : 2,
        "countryName" : "India",
        "phoneCode" : null,
        "sortName" : null
    }
]

修改

我的输入是一个json数组,错误是显而易见的,然后我像这样更改了json文件

{"countries":[
    {
        "countryId" : 1,
        "countryName" : "Any",
        "phoneCode" : null,
        "sortName" : null
    },
    {
        "countryId" : 2,
        "countryName" : "India",
        "phoneCode" : null,
        "sortName" : null
    }
]
}

当我再次尝试时,我在控制台中得到了这个

Error writing document:  { Error: Missing or insufficient permissions.
    at /home/me/Documents/TfmFirebase/functions/node_modules/grpc/src/client.js:554:15 code: 7, metadata: Metadata { _internal_repr: {} } }
Error writing document:  { Error: Missing or insufficient permissions.
    at /home/me/Documents/TfmFirebase/functions/node_modules/grpc/src/client.js:554:15 code: 7, metadata: Metadata { _internal_repr: {} } }

我添加了一个服务帐户并在脚本中添加了json。这有什么问题?

1 个答案:

答案 0 :(得分:0)

我忘了在创建服务帐户时设置角色,删除旧帐户并创建具有编辑角色的新服务帐户,并在脚本中添加新的json。现在它可以工作