From 00a53aeca8707d71e1d48ba3a0a0068e0add4ba6 Mon Sep 17 00:00:00 2001 From: Abhishek Sai Date: Sun, 21 Oct 2018 22:13:46 +0530 Subject: [PATCH] checking if school already exists while registering school --- functions/index.js | 31 +++++++++++++++++++++---------- 1 file changed, 21 insertions(+), 10 deletions(-) diff --git a/functions/index.js b/functions/index.js index 86b87dc..1e271cf 100644 --- a/functions/index.js +++ b/functions/index.js @@ -80,17 +80,28 @@ app.post('/onSubmit', (req, res) => { var mobile = req.body.mobile; var cheque = req.body.cheque; console.log(schoolName + " " + facultyName + " " + email + " " + mobile); - var ob = { - cheque : cheque, - schoolName: schoolName, - Faculty: { - ContactNo: mobile, - EmailID: email, - Name: facultyName, + var sch = db.collection("Schools").doc(schoolId); + sch.get().then((doc) => { + if(doc.exists){ + return(doc); + }else{ + var ob = { + cheque : cheque, + schoolName: schoolName, + Faculty: { + ContactNo: mobile, + EmailID: email, + Name: facultyName, + } + + }; + db.collection("Schools").doc(schoolId).set(ob); + throw new Error("School does not exist"); } - - }; - db.collection("Schools").doc(schoolId).set(ob); + }).catch((err)=>{ + console.log(err); + }); + // db.ref('Schools/'+schoolId).set({ // cheque : cheque, // schoolName: schoolName,