Reputation: 829
I have new variable called var lat and var long in index.js. For getting firebase value, I have used this code
var lat;
var long;
var cityRef =
db.collection('UsersCurrentBookings').doc('3Xg6ujHMINeElzdOD3JypcZBMHw2');
var getDoc = cityRef.get()
.then(doc => {
if (!doc.exists) {
console.log('No such document!');
} else {
console.log('Document data:', doc.data());
}
})
.catch(err => {
console.log('Error getting document', err);
});
My console output:
Document data: { city: 'Chennai',
country: 'India',
time: '10:35:57',
date: '2018-03-28',
token: 'deFFa4dBPlQ:APA91bF7hTjsSkk-l6fLaI939W0ISe2olO18OjCUZu4N9URr9QKW4xXBIyWyC1L9Vui1cawGkFaSV1M2yDG4ASJyeCtrwGyal0GmIgdciZeGVmNSUnvyddTLUPAxQlAsjpAHTJy4Lwqt',
address: 'Sri Aravinthar St, Sholinganallur, Chennai, Tamil Nadu 600119, India',
Currentlat: '12.8710994',
Currentlong: '80.2225558',
I want pass console output value currentlat and currentlong value into new variable of lat and long.
After getting value from UsersCurrentBookings db then how to insert value into db.collection(DriversCurrentBookins) and parameter value lat, long and token.
Any help will be greatly appreciated! Thanks in advance!!!
Upvotes: 0
Views: 2401
Reputation: 829
called set data into usercurrentbooking collection db
var cityRef =
db.collection('UsersBookingRequest').doc('9DjekLkfFmTeLfkyZQAeezVriv02');
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);
});
Upvotes: 0
Reputation: 1077
You can directly access data from the JSON response and if you want, you can parse the data to Float type too using Javascript Global function parseFloat();
var lat = parseFloat(doc.data().Currentlat);
var lng = parseFloat(doc.data().Currentlong);
****UPDATE****
To add data to your Mongo database, you can use libraries like
MongooseJS : // This uses a model based structure, can be a little difficult to understand at first.
or
****UPDATE Ignore previous one****
const tokenField = data.getData().token
const ref = db.collection().doc(tokenField);
var jsonData = {
'lat' : lat,
'lng' : lng
}
ref.set(jsonData);
Upvotes: 2