Javascript Firestore参考资料
我想使用nodejs将文档中的引用字段添加到Firestore中,但我做不到 我写了这段代码:Javascript Firestore参考资料,javascript,node.js,firebase,google-cloud-firestore,Javascript,Node.js,Firebase,Google Cloud Firestore,我想使用nodejs将文档中的引用字段添加到Firestore中,但我做不到 我写了这段代码: async function pushType(IDType) { const size = await getID(); const IDClient = (size).toString(); const docRef = firebase.firestore().collection('Clients').doc(IDClient); await docRef.set({
async function pushType(IDType) {
const size = await getID();
const IDClient = (size).toString();
const docRef = firebase.firestore().collection('Clients').doc(IDClient);
await docRef.set({
ID_Type: firebase.firestore().doc('Types/'+IDType).ref,
});
}
async function getID(){
const snapshot = await firebase.firestore().collection('Clients').get();
return snapshot.size;
}
错误是:“使用无效数据调用了函数Document.Reference.set()。不支持的字段值:未定义(在Document Clients/7中的字段ID_Type中找到)”,其中7是要添加字段ID_Type的文档的ID
有谁能帮助我理解我错了什么,我该如何纠正它?谢谢您的代码
firebase.firestore().doc('Types/'+IDType)
返回一个。正如您从API文档中看到的那样,该文件上没有名为ref
的属性,因此它将是未定义的。事实上,DocumentReference正是您希望在此处提供给Firestore的内容。所以只需删除ref
:
await docRef.set({
ID_Type: firebase.firestore().doc('Types/'+IDType)
});
当您向Firestore文档提供这样的DocumentReference对象时,它将创建一个引用类型字段。您的代码
firebase.Firestore().doc('Types/'+IDType)
返回一个引用类型字段。正如您从API文档中看到的那样,该文件上没有名为ref
的属性,因此它将是未定义的。事实上,DocumentReference正是您希望在此处提供给Firestore的内容。所以只需删除ref
:
await docRef.set({
ID_Type: firebase.firestore().doc('Types/'+IDType)
});
当您像这样向Firestore文档提供DocumentReference对象时,它将创建一个引用类型字段