-1

I have a datastructure in the form of object structure . [![enter image description here][1]][1]

{
  Invite1
  {
     Amount: 10,
     PhoneNumber:9876543210,
     Status:"Pending"
  }
  Invite2
  {
     Amount: 20,
     PhoneNumber:1234566789,
     Status:"Pending"
  }
}

I have a condition when whose Invite(1,2,3) PhoneNumber matches with other document that invitee need to update the field as Status = true
When I try to update a field as Status = true It is updating at the end of the document. Mycode need to update

var dbref = db1.collection('deyaPayUsers').doc(sendauthid).collection('Split').doc(sendauthid).collection('SentInvitations').doc(senderautoid);
var msg1 = receiverph + "" + status + " to pay $" + document.Amount;
var fulldoc = dbref.get()
    .then(doc => {
        if (!doc.exists) {
            console.log('No such document');
        } else {
            console.log('Document data :', doc.data());
            d1 = doc.data();
            console.log("d1 is" + d1);
            for (var k in d1) {
                var p = d1[k].PhoneNumber;
                console.log("ivitees phone" + p);
                if (receiverph == p) // Here the condition is true of the invite phoneNumber then need to update
                {
                    console.log("p" + PhoneNumber);


                    console.log("the phonenumber matches");

var updated = dbref.update({"Status":status});// Here It is updating at the endof the document

other Method to update

d1.Status = status; // In d1 I have the document data var setdata = dbref.set(d1);

Please if their is any approach help with me. Thanks

Renaud Tarnec
  • 79,263
  • 10
  • 95
  • 121
vijju
  • 462
  • 9
  • 30
  • Could you please share the code you have already written and where you are encountering problems/errors. Also, is the field Status in the same document than the 3 InviteX fields? – Renaud Tarnec Apr 24 '18 at 11:51
  • I need to update the status in any invite if my condition is true. K I will share my piece of code – vijju Apr 24 '18 at 12:09
  • Ok, thanks for the code. When you say that "updating at the end of the document" is not what you are looking for, it means that you want to update each Invite field in the document. Is this right? – Renaud Tarnec Apr 24 '18 at 12:32

1 Answers1

2

If I understand correctly that you would like to update each InviteXX item in the document, here is a code that will work (I've kept the main part of your code):

var dbref = db1.collection('deyaPayUsers').doc(sendauthid).collection('Split').doc(sendauthid).collection('SentInvitations').doc(senderautoid);
var msg1 = receiverph +"" + status +" to pay $"+document.Amount;
const fulldoc = dbref.get()
    .then(doc => {
        if (doc.exists) {
            //console.log("Document data:", doc.data());

            const inviteUpdate = {};  //An object that we will update by looping over the InviteXX objects of the document

            const d1 = doc.data();
            //console.log("d1 is" + d1);

            for (let key in d1) {
                if (d1.hasOwnProperty(key)) {
                    const p = d1[key].PhoneNumber;
                    //console.log(key);
                    //console.log("invitees phone " + p);
                    if (receiverph === p) // Here the condition is true of the invite phoneNumber then need to update
                    {
                        inviteUpdate[key + '.status'] = true;
                        //The key point is here: we define the object field with a mix of dot notation and []
                    }
                }
            }

            return dbref.update(inviteUpdate);

        } else {
            // doc.data() will be undefined in this case
            console.log("No such document!");
            throw "No such document"
        }
    })
    .catch(function (error) {
    console.log("Error:", error);
});

Note that:

  1. You should return a promise in your Cloud Function. I don't have the full code of your function but probably you will return fulldoc. (You may have a look at this video https://www.youtube.com/watch?v=652XeeKNHSk&t=2s)
  2. I don't know how do you get/initialize receiverph
  3. You may use const or let instead of var in a Cloud Function (JavaScript ES6+)
Renaud Tarnec
  • 79,263
  • 10
  • 95
  • 121