0

Now i can able to get doc id and insert data into DriversCurrentBookings. When new currenUserRequest doc id is added in collection db, it should update in realtime in firebase (using cloud firestore)

Here is the code used:

var citiesRef = db.collection('UsersBookingRequest');
var allCities = citiesRef.get()
.then(snapshot => {
    snapshot.forEach(doc => {
        console.log(doc.id, '=>', doc.data());
        var reqID = doc.data().UID;

        var cityRef = db.collection('UsersBookingRequest').doc(reqID);
        var getDoc = cityRef.get()
             .then(doc => {
                 if (!doc.exists) {
                     console.log('No such document!');
                 } else {
                        console.log('Document data:', doc.data());
                        lat = doc.data().Currentlat;
                        long = doc.data().Currentlong;
                        UID = doc.data().UID;
                        // token = doc.data().token;
                        address = doc.data().address;
                        date = doc.data().date;
                        time = doc.data().time;

                        console.log(lat);
                        console.log(long);
                        console.log(UID);
                        // console.log(token);
                        console.log(address);
                        console.log(date);
                        console.log(time);

        var data = {

                 lat: lat,
                 long: long,
                 UID: UID,
                 address: address,
                 date: date,
                 time: time
                 // token: token


                    };

                // Add a new document in collection "cities" with ID 'LA'
        var setDoc = 
db.collection('DriversCurrentBookings').doc(doc.data().UID).set(data);

    }
})
.catch(err => {
    console.log('Error getting document', err);
});

    })
})
.catch(err => {
    console.log('Error getting documents', err);
});

Here is the screenshot of db enter image description here enter image description here

PvDev
  • 791
  • 21
  • 67

1 Answers1

1

You can easily get the id of an added document by calling its .id or .ref (if you want the whole reference to store in another location).

Example 1

return db.collection('myCollection').add(someData)
  .then(response => {
    console.log(`Document ID: ${response.id}`);
  })
  .catch(err => {
    console.error(err);
  });

Example 2

let docRef = db.collection('myCollection').doc();
console.log (`Document ID: ${docRef.id}`);

return docRef.set(someData)
  .then(response => {
    console.log('The data was saved');
  })
  .catch(err => {
    console.error(err);
  });
Jason Berryman
  • 4,760
  • 1
  • 26
  • 42
  • instead adding static data db.collection('UsersBookingRequest').doc('9DjekLkfFmTeLfkyZQAeezVriv02'); in doc id, I want to add dynamic id – PvDev Apr 04 '18 at 14:17
  • Yes. My example shows adding a document with a dynamic ID and then getting the ID from the response. `.add` adds a document to a collection and creates a dynamic ID. You then retrieve it, by getting the `.id` property of the response. – Jason Berryman Apr 04 '18 at 14:59
  • I've added a second example, as an alternative option. Example 1 saves the document and then gets the ID. Example 2 creates an ID and then saves the document – Jason Berryman Apr 04 '18 at 15:04