I use this function to send data to firestore
exports.professional = functions.https.onRequest((request, response) => {
const db = admin.firestore();
const userId: string = request.body['userId'];
const qualificationId: number = request.body['qualificationId'];
const occupationId: number = request.body['occupationId'];
const employmentType: EMPLOYMENT_TYPE = request.body['employmentType'];
const collegeOrInstitution: string = request.body['collegeOrInstitution'];
return db.collection('profiles').doc(userId)
.set(
{
professional: {
qualifaicationId: qualificationId,
occupationId: occupationId,
employmentType: employmentType,
collegeOrInstitution: collegeOrInstitution
}
},
{
merge: true
}).then(writeResult => {
return response.json(
{
status: 1,
message: 'Professional details saved successfully',
result: userId
}
)
});
});
I have declared some variables as number but when i check the firestore doc, it is saved as strings. please see the screenshot
In the code, i have declared occupationId
as a number variable but after it is saved it is a string, again please see the screenshot. Anyone know why it is changing data types?
Number values are saved as strings in firestore.
Firestore now has a specific operator for this called FieldValue. increment() . By applying this operator to a field, the value of that field can be incremented (or decremented) as a single operation on the server.
Note: Cloud Firestore supports a variety of data types for values: boolean, number, string, geo point, binary blob, and timestamp. You can also use arrays or nested objects, called maps, to structure data within a document.
Declarations, e.g.:
const occupationId: number
by themselves do not convert passed data in case if the assignment type is any
(it's done so that it was possible to gradually switch from js to ts).
If you need to have a number explicitly - you need to use something that converts the actual value type for you.
One possible way to do that:
const occupationId: number = Number(request.body['occupationId']);
References:
Number
- Convert numeric strings to numbersIf you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With