Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Number values are saved as strings in firestore

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

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?

like image 535
Sony Avatar asked Feb 14 '18 20:02

Sony


People also ask

Which of the following types are stored as string type in cloud firestore?

Number values are saved as strings in firestore.

How do I add numbers to 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.

What data is supported in the firestore?

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.


1 Answers

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:

  • MDN: Number - Convert numeric strings to numbers
like image 189
zerkms Avatar answered Sep 24 '22 01:09

zerkms