Home > database >  Firestore - Store a property id is reference type by firestore Batch transaction
Firestore - Store a property id is reference type by firestore Batch transaction

Time:03-09

I trying to store data content reference type by batch transaction, then I got an exception:

Function WriteBatch.set() called with invalid data. Unsupported field value: a custom object (found in document orders/OC9dZErupEhPsamp8QEd)

Is there a way we can use batch transaction to store reference type?

this is my code:

batch.update(orderRef, {
    userId: firestore.doc(userId),
});

CodePudding user response:

Normaly update() use to update existing firestore data. Review firestore docs for the same. In that given example they are updating population by increments of value or with new population number but before passing it in each update function values are stored in one cost value if it is not static value. as Asked by @dharmaraj please edit your questions by posting with full code you can also read given firestore documentation for your own studies.

    import firebase from "firebase/app";  
    const app = firebase.initializeApp({});
     const firestore = app.firestore(); 
    const batch = firestore.batch(); 
    const newUserId = firestore.doc(userId);
    batch.update(orderRef, {
     userId: newUserId,
    });

Log newUserId value and see what are you getting into it.

CodePudding user response:

You can't store the reference object that doc() returns, it's an object that may have circular references and functions in it. doc() is not the id of the document. If you want to get the id (which is a string), then:

const newUserId = firestore.doc(userId).ref.id;
batch.update(orderRef, {
 userId: newUserId,
});

  • Related