错误 [FirebaseError]:文档参考无效。文档引用必须有偶数个段,

时间:2021-01-23 13:51:39

标签: node.js google-cloud-firestore

const firebase = require("firebase");
// Required for side-effects
require("firebase/firestore");

// Initialize Cloud Firestore through Firebase
firebase.initializeApp({
    apiKey: "____",
    authDomain: "____",
    projectId: "____"
  });
  
var db = firebase.firestore();

var datasetSymptom=[{
    "Disease" : [ "edema pulmonary" ],
    "Symptom" : "Heberden's node"
  }, {
    "Disease" : [ "cholecystitis" ],
    "Symptom" : "Murphy's sign"
  }, {
    "Disease" : [ "hemiparesis", "hypertension pulmonary", "transient ischemic attack" ],
    "Symptom" : "Stahli's line"
  }
  ]

datasetSymptom.forEach(function(obj) {
    db.collection("datasetSymptom").doc(obj.Symptom).set({
        Disease: obj.Disease
    })
});

我已经在另一个集合上运行了相同的算法,名称与文档相同,症状数组与字段相同。它工作得很好。 错误:

Disease

///////////////////////////////////////////// ///////////////////////////////////////////////// ///////////////////////////////////////////////

1 个答案:

答案 0 :(得分:0)

您的数组中的一个或多个对象似乎没有 Symptom 属性。当这种情况发生时,doc(obj.Symptom) 会导致对空文档的引用,这就是导致错误的原因。

要查找有问题的数组项,请手动检查您的 JSON,或在您的代码中执行以下操作:

datasetSymptom.forEach(function(obj, index) {
  if (obj.Symptom && object.Symptom.length) {
    db.collection("datasetSymptom").doc(obj.Symptom).set({
      Disease: obj.Disease
    })
  }
  else {
    console.error(`Found an object without a Symptom at index ${index} in the array`);
  }
});
相关问题