简体   繁体   中英

Firestore References

i want to add a reference field in a document into Firestore using nodejs but i can't do it.

I wrote this code:

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

The error is: "Function Document.Reference.set() called with invalid data. Unsupported field value: undefined (found in field ID_Type in document Clients/7)" where 7 is the ID of the document where i want to add the field ID_Type.

Can anyone help me to understand what i'm wrong and how can i fix it? Thank you

Your code firebase.firestore().doc('Types/'+IDType) returns a DocumentReference . As you can see from the API docs, there is no property called ref on that, so it will be undefined. In fact, that DocumentReference is exactly what you want to provide to Firestore here. So just remove ref :

  await docRef.set({
    ID_Type: firebase.firestore().doc('Types/'+IDType)
  });

When you provide a DocumentReference object to a Firestore document like this, it will create a reference type field.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM