问题
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:\react tutorial\reactnative\fyp\node_modules\@firebase\firestore\dist\node-cjs\database-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:\react tutorial\reactnative\fyp\node_modules\←[4m@firebase←[24m\firestore\dist\node-cjs\database-21da52c3-e2da0fdd.js:209:28)
at validateDocumentPath (D:\react tutorial\reactnative\fyp\node_modules\←[4m@firebase←[24m\firestore\dist\node-cjs\database-21da52c3-e2da0fdd.js:19706:15)
at doc (D:\react tutorial\reactnative\fyp\node_modules\←[4m@firebase←[24m\firestore\dist\node-cjs\database-21da52c3-e2da0fdd.js:22462:9)
at CollectionReference$1.doc (D:\react tutorial\reactnative\fyp\node_modules\←[4m@firebase←[24m\firestore\dist\node-cjs\database-21da52c3-e2da0fdd.js:25864:64)
at D:\react tutorial\reactnative\fyp\index2x2.js:1233:37
at Array.forEach (<anonymous>)
at Object.<anonymous> (D:\react tutorial\reactnative\fyp\index2x2.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
}
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
回答1:
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`);
}
});
来源:https://stackoverflow.com/questions/65859965/error-firebaseerror-invalid-document-reference-document-references-must-have