Thursday 5 September 2019

Firestore cloud function add multiple fields in one field

I have a field names 'Options' and this field contains in itself 8 different checkboxes values. I am writing firestore cloud function using nodejs to post data in the collection where this field 'Options' is present. So far this is what i have written

exports.addCourse = functions.https.onRequest((req, res) => {
    res.set('Access-Control-Allow-Origin', 'http://localhost:4200');
    res.set('Access-Control-Allow-Methods', 'GET', 'POST');
    res.set('Access-Control-Allow-Headers', 'Content-Type'); 
    if(req.method === 'OPTIONS') {
        res.end();
    }
    else 
    {
        if (req.body.name != null && req.body.type != null && req.body.discipline != null && req.body.price_options != null
            && req.body.price != null && req.body.addon_prompt != null && req.body.addons != null && req.body.ship_price != null
            && req.body.keycode_bank != null && req.body.card_type != null && req.body.options != null && req.body.certificate_prompt != null
            && req.body.student_to_instructor_ratio != null && req.body.student_to_manikin_ratio != null && req.body.electronic_signature != null
            || req.body.name != undefined && req.body.type != undefined && req.body.discipline != undefined && req.body.price_options != undefined
            && req.body.price != undefined && req.body.addon_prompt != undefined && req.body.addons != undefined && req.body.ship_price != undefined
            && req.body.keycode_bank != undefined && req.body.card_type != undefined && req.body.options != undefined && req.body.certificate_prompt != undefined
            && req.body.student_to_instructor_ratio != undefined && req.body.student_to_manikin_ratio != undefined && req.body.electronic_signature != undefined ) {
            let docId = Math.floor(Math.random() * (99999 - 00000));
            let newCourse = {
                "name": req.body.name,
                "type": req.body.type, //1: Classroom session enabled 2. No Classroom session 
                "discipline": req.body.discipline,
                "price_options": req.body.price_options, //Registrations allowed with deposits or not?
                "price": req.body.price,
                "addon_prompt": req.body.addon_prompt,
                "addons": req.body.addons, //add addons api values will be sent here 
                "ship_price": req.body.ship_price,
                "keycode_bank": req.body.keycode_bank, //Keycode bank value will be sent here
                "card_type": req.body.card_type, //Card type value will be sent here
                "options": {
                    "certificate_prompt": req.body.certificate_prompt,
                    "student_to_instructor_ratio": req.body.student_to_instructor_ratio,
                    "student_to_manikin_ratio": req.body.student_to_manikin_ratio,
                    "electronic_signature": req.body.electronic_signature
                }
            }
            usersCourses.add(newCourse).then(snapshot => {
                res.send(200, {
                    "message": "Course was successfully created"
                })
            });


        } else {
            res.send(400, {
                "message": "All fields are required"
            })
        }
    }
});

Deployed this API and then hitted it with Postman but got an error that 'All field are required'. What am i doing wrong? Is this the right way to add multiple fields in 'Options' field??

Data that i am sending through postman is not real one as ther are no specific datatypes provided to properties in the API

Data

{
    "name": "req.body.name",
    "type": "req.body.type", 
    "discipline": "req.body.discipline",
    "price_options": "req.body.price_options",
    "price": "req.body.price",
    "addon_prompt": "req.body.addon_prompt",
    "addons": "req.body.addons",
    "ship_price": "req.body.ship_price",
    "keycode_bank": "req.body.keycode_bank", 
    "card_type": "req.body.card_type", 
    "options": {
        "certificate_prompt": "req.body.certificate_prompt",
        "student_to_instructor_ratio": "req.body.student_to_instructor_ratio",
        "student_to_manikin_ratio": "req.body.student_to_manikin_ratio",
        "electronic_signature": "req.body.electronic_signature"
    },
    "ceu_credits": "req.body.ceu_credits",
    "description": "req.body.description",
    "confirm_email": "req.body.confirm_email"

}



from Firestore cloud function add multiple fields in one field

No comments:

Post a Comment