Reputation: 683
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"
}
Upvotes: 2
Views: 654
Reputation: 1429
The problem is in your if
condition you are checking for values that come in the options
property as if they came in the top level of the data you are sending on the request body.
Also you can just check for null
values as you are using !=
operator without type coercion.
If the data you are sending is the one you provided with the question, change your if
condition content to (checking for values inside the options
field, removing undefined
checks):
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.options.certificate_prompt != null
&& req.body.options.student_to_instructor_ratio != null && req.body.options.student_to_manikin_ratio != null && req.body.options.electronic_signature != null
and change where you create the newCourse
variable to:
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.options.certificate_prompt,
"student_to_instructor_ratio": req.body.options.student_to_instructor_ratio,
"student_to_manikin_ratio": req.body.options.student_to_manikin_ratio,
"electronic_signature": req.body.options.electronic_signature
}
}
Now you can send the example 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"
}
with postman and it should work.
Upvotes: 3