0

I have a doc in my collection in this format

name: xyz,
email: xyz@email.com,
age: 30,
address: {
    street_no: {
        complete_address: somedata,
        pincode: somepin
    },
    city:somecity,
    state:somestate,
    landmark:nearby
}

And inside this doc I am trying to insert and merge the complete_address with the previous record. To achieve that I am trying this

const database = firebase.firestore();
var dataRef = database.collection('collection');
var query = dataRef.doc(key+"").get().then(doc=>{
    if(!doc.exists){
        res.send("doesn't exist");
    }else{
        //few checks
        if(doc.data().accessid != accessid){
            res.send("accessid doesn't match")
        }
        //here I am trying to insert and merge with the previous data
        var form_path = 'address.street_no.complete_address';
        dataRef.doc(key+"."+form_path).set({
            another_address
        }, {merge: true});
    }
}).catch(err=>{
    console.log(err)
})

But when I execute this it just add another document in a collection followed by this path key.address.street_no.complete_address.

What can I do to only insert and merge with the previous complete_address ?

There is . instead of / in form_path because got few ideas from this link

Lokesh Pandey
  • 1,739
  • 23
  • 50

2 Answers2

2

I believe your issue lies within the next couple of lines starting at

var form_path = 'address.street_no.complete_address';

Next, You're using dataRef.doc(key+"."+form_path)

which means the only document being set is

/addressCollection/key.{addressCollectionId}

and addressCollectionId being address.street_no.complete_address

Instead what you want to do is access the property within the document using dot notation like so.

address: {
    street_no: {
        complete_address

Example.

someDocument.update({
    "address.street_no.complete_address": "some_data"
});

Note that "some_data" will replace what ever data is currently stored. You'll want to do one read and merge the data. For example.

  const anotherAddress = { address: "123 Fake Street" };

  const document = firebase
    .firestore()
    .collection("addressCollection")
    .doc("someAddressId");

  document
    .get()
    .then(snap => {
      const data = snap.data();
      const completeAddress = data.address.street_no.complete_address };

      // We're using the spread operator here to combine the current completeAddress with anotherAddress
      return { completeAddress, ...anotherAddress };
    })
    .then(newCompleteAddress =>
      document.update({
        "address.street_no.complete_address": newCompleteAddress
      })
    );
Philip
  • 841
  • 6
  • 13
0

I got this working.

So I figured out what I was trying to do earlier will create another document in a collection with data respect to it. So I start treated everything as an object and passed an object data to set() method.

const database = firebase.firestore();
var dataRef = database.collection('collection');
var query = dataRef.doc(key+"").get().then(doc=>{
    if(!doc.exists){
        res.send("doesn't exist");
    }else{
        //few checks
        if(doc.data().accessid != accessid){
            res.send("accessid doesn't match")
        }
        //here I am trying to insert and merge with the previous data
        var mergeData = {
            address : {
            }
        }
        var new_address = {
            key: "address_data"
        }
        mergeData.address[street_no] = {complete_address : address}
        if(dataRef.doc(key+"").set(mergeData, {merge: true})){
            res.send("done")
        }else{
            res.send("failed")
        }
    }
}).catch(err=>{
    console.log(err)
})
Lokesh Pandey
  • 1,739
  • 23
  • 50