firebaseを利用したサーバサイドの開発をしており、非同期処理を同期的に直す過程でエラーが出ました。
データをajaxで受け取る→それをfirebaseのデータベースと突合→合致する場合はresponse(500)を帰す
というバックエンドの処理をしようとしています。
「データを格納→完了後に突合」としたく、async/awaitの非同期処理の最後の例にならって、
promise,async,awaitを用いた非同期処理の待ちを実装しましたが、下記のエラーが出ます。
65:9 error Parsing error: Unexpected token function
node.js
1/////////////////////////////////////////index.js////////////////////////////////// 2 3const functions = require('firebase-functions'); 4// // Create and Deploy Your First Cloud Functions 5// // https://firebase.google.com/docs/functions/write-firebase-functions 6// The Firebase Admin SDK to access the Firebase Realtime Database. 7 8// Import Admin SDK 9const admin = require('firebase-admin'); 10admin.initializeApp(functions.config().firebase); 11 12// Get a database reference to our posts 13var db = admin.database(); 14var ref = db.ref("/product"); 15 16//chromeExtensionからのデータを受信 17exports.product = functions.https.onRequest((request, response) => { 18 //POSTを受けると発火 19 if (request.method === "POST") { 20 const body = request.body; 21 const pushRef = admin.database().ref("/product").push(); 22 pushRef.set({ 23 //薬処方時のデータ 24 patientId: body["patientId"], 25 doctor: body["doctor"], 26 idisease: body["idisease"], 27 medicine: body["medicine"], 28 medicineID:body["medicineID"], 29 }, error => { 30 if (error) { 31 console.log("save error", error.message); 32 } else { 33 console.log("save Success!!"); 34 } 35 } 36 ); 37 }//ajaxの受け取り 38 39//////////////////////////////////////////////////////////////////// 問題個所はここ以降 40 //medicineIDを薬マスタと突合 41 42 //薬のIDを配列に格納 43 function getInMedId() { 44 const p = new Promise((resolve,reject) => { 45 var inMedId = body.medicineID; 46 resolve(inMedId); 47 }); 48 return p; 49 } 50 51 //マスター側の薬のIDを配列に格納 52 function getPreMedId() { 53 const q = new Promise((resolve,reject) => { 54 var preMedId = []; 55 for(j=0; j<(90+1); j++){ 56 var m = db.ref("/"+j+"/medicineId"); 57 m.on("value", function(snapshot){ 58 preMedId = snapshot.val(); 59 }); 60 } 61 resolve(preMedId); 62 }); 63 return q; 64 } 65 66 //薬のIDを突合 67 async function checkMedId() { <============================================ ※エラー箇所※ 68 for(i=0; i<inMedId.length+1; i++){ 69 for(j=0; j<(90+1); j++){ 70 var a = await getInMedId(); 71 var b = await getPreMedId(); 72 if(a[0][i] === b[j]){ 73 console.log('This medicine is skeptical:'+a); 74 response.status(500).send(error.message).end(); 75 } 76 } 77 } 78 } 79 checkMedId(); 80 //for回して無事だったら普通のレスポンス 81 response.status(200).end(); 82 83}) 84

バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2018/03/08 07:07
2018/03/08 07:36