Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/node.js/33.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript Firestore参考资料_Javascript_Node.js_Firebase_Google Cloud Firestore - Fatal编程技术网

Javascript 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({

我想使用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({
    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对象时,它将创建一个引用类型字段