Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
599 views
in Technique[技术] by (71.8m points)

node.js - Error [FirebaseError]: Invalid document reference. Document references must have an even number of segments,

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
    })
});

I already ran the same algo on another collection with same Disease name as doc and array of symptoms as field. It worked fine. Error:

D:
eact tutorial
eactnativefyp
ode_modules@firebasefirestoredist
ode-cjsdatabase-21da52c3-e2da0fdd.js:25868
            throw replaceFunctionName(e, 'doc()', 'CollectionReference.doc()');
            ^

Error [FirebaseError]: Invalid document reference. Document references must have an even number of segments, but datasetSymptom/feels hot/feverish has 3.
    at new FirestoreError (D:
eact tutorial
eactnativefyp
ode_modules←[4m@firebase←[24mfirestoredist
ode-cjsdatabase-21da52c3-e2da0fdd.js:209:28)
    at validateDocumentPath (D:
eact tutorial
eactnativefyp
ode_modules←[4m@firebase←[24mfirestoredist
ode-cjsdatabase-21da52c3-e2da0fdd.js:19706:15)
    at doc (D:
eact tutorial
eactnativefyp
ode_modules←[4m@firebase←[24mfirestoredist
ode-cjsdatabase-21da52c3-e2da0fdd.js:22462:9)
    at CollectionReference$1.doc (D:
eact tutorial
eactnativefyp
ode_modules←[4m@firebase←[24mfirestoredist
ode-cjsdatabase-21da52c3-e2da0fdd.js:25864:64)
    at D:
eact tutorial
eactnativefypindex2x2.js:1233:37
    at Array.forEach (<anonymous>)
    at Object.<anonymous> (D:
eact tutorial
eactnativefypindex2x2.js:1231:16)
←[90m    at Module._compile (internal/modules/cjs/loader.js:1063:30)←[39m
←[90m    at Object.Module._extensions..js (internal/modules/cjs/loader.js:1092:10)←[39m
←[90m    at Module.load (internal/modules/cjs/loader.js:928:32)←[39m {
  code: ←[32m'invalid-argument'←[39m,
  toString: ←[36m[Function (anonymous)]←[39m
}

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

question from:https://stackoverflow.com/questions/65859965/error-firebaseerror-invalid-document-reference-document-references-must-have

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

It looks like one or more of the objects in your array don't have a Symptom property. When that happens doc(obj.Symptom) results in a reference to an empty document, which is what causes the error.

To find the problematic array item, either check your JSON manually, or do something like this in your code:

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`);
  }
});

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...