【发布时间】:2019-07-30 19:31:44
【问题描述】:
我已经坚持了几个月。我已经从函数中删除了一些小细节,但没有什么大不了的。我有这个 https 云函数,它结束一个会话,然后使用 endTime 和 startTime 计算 bill 然后返回给客户端。
startTime 是从实时 firebase 数据库(会话启动器函数放在那里)中检索出来的。
我的代码sn-p:
exports.endSession = functions.https.onRequest(async (req, res) => {
console.log("endSession() called.")
if(req.method == 'GET'){
bid = req.query.bid
session_cost = req.query.sessioncost
}else{
bid = req.body.bid
session_cost = req.body.sessioncost
}
start_time_ref = admin.database().ref("/online_sessions/").child(bid).child("start_time")
start_time_snapshot = await start_time_ref.once('value')
console.log("start_time_snapshot: "+start_time_snapshot.val())
start_time_snapshot = moment(start_time_snapshot.val(), 'dddd MMMM Do YYYY HH:mm:ss Z');
endDateTime = getDateTime()
console.log("startTime: " + start_time_snapshot.toString())
console.log("endTime: " + endDateTime.toString())
hour_difference = getHourDifference(start_time_snapshot, endDateTime)
bill = ride_cost * Math.ceil(hour_difference)
console.log("bill: "+bill)
var s_phone
sSessionlinks_ref = admin.database().ref('/sSessionlinks/')
sSessionlinks_snapshot = await sSessionlinks_ref.once('value')
sSessionlinks_snapshot.forEach((sid)=>{
if(sid.val() == bid){
s_phone = sid.key
}
})
s_fcm_token_ref = admin.database().ref("/s/").child(s_phone).child("FCM")
s_fcm_token_snapshot = await s_fcm_token_ref.once('value')
try{ // telling another client that session has ended.
await admin.messaging().send({
data: {
type: "sessionCompleted",
bill: bill.toString()
},
token: s_fcm_token_snapshot.val()
})
}catch(error){
}
//deleting this session from online sessions
online_session_ref = admin.database().ref('/online_sessions/').child(bid)
await online_session_ref.remove()
//puting this session as available
available_session_ref = admin.database().ref('/available_sessions/')
json = {}
json[bid] = s_phone
await available_session_ref.update(json) // session made available
res.status(200).send(bill.toString())
// here it *sometimes* returns 304 and then restarts but since i've already removed online_session_ref I cannot get startTime again because its removed with online_sessions so it fails.
// return
})
当它第一次调用时。它正确地完成了所有计算,但响应为 304。因此(我认为客户端)重新发送请求并再次调用该函数,但由于会话被破坏,因此它无法计算 startTime。
为什么当它第一次被调用时,即使所有的计算都正确发生,它返回一个 304 而不是一个 200?这个问题不会一直发生。通常在很长一段时间后调用此函数时会发生这种情况,但我不确定。我不知道是什么原因造成的。
我用过的辅助函数:
function getHourDifference(s, e){
return moment.duration(e.diff(s)).asHours()
}
function getDateTime(){
d = moment.utc().utcOffset('+0530')
return d
}
函数第一次结束时,文本载荷为Function execution took 794 ms, finished with status code 304
当它第二次运行时(它无法获得startTime,因为它在第一次运行时被删除。首先不应该第二次运行。),有效负载文本是Function execution took 234 ms, finished with status code 200(它的200但返回NaN 因为没有startTime 就无法进行计算。
编辑: 正如你们中的一些人要求我说明该函数是如何被调用的:
它是从使用 Volley 的 android 应用程序调用的。保证参数不为空。调用该函数的代码段是:
// Setting the button's listeners.
endSessionButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
progressDialog = new SweetAlertDialog(getContext(), SweetAlertDialog.PROGRESS_TYPE);
progressDialog.getProgressHelper().setBarColor(Color.parseColor("#A5DC86"));
progressDialog.setTitleText("Ending session...");
AlertDialog endDialog = new AlertDialog.Builder(getContext()).create();
endDialog.setTitle("End Session?");
Log.e("sessioncost", String.valueOf(session_cost));
endDialog.setButton(Dialog.BUTTON_POSITIVE, "Yes", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
progressDialog.show();
// Instantiate the RequestQueue.
RequestQueue queue = Volley.newRequestQueue(getContext());
String url = "https://us-central1-something-something.cloudfunctions.net/endSession?bid=" + bid + "&sessioncost=" + session_cost;
Log.e("end sesion button", url);
// Request a string response from the provided URL.
StringRequest endSessionRequest = new StringRequest(Request.Method.GET, url,
new Response.Listener<String>() {
@Override
public void onResponse(final String response) {
progressDialog.dismiss();
Toast.makeText(getContext(), "Session Completed", Toast.LENGTH_LONG).show();
progressDialog = new SweetAlertDialog(getContext(), SweetAlertDialog.SUCCESS_TYPE);
progressDialog.getProgressHelper().setBarColor(Color.parseColor("#A5DC86"));
progressDialog.setTitleText("Session Completed: Bill");
progressDialog.setContentText("Please pay ?" + response + " to s.");
progressDialog.setCancelable(false);
progressDialog.show();
changeState('1');
bill_in_paise = Float.parseFloat(response) * 100;
Log.e("bill", bill_in_paise.toString());
progressDialog.setConfirmClickListener(new SweetAlertDialog.OnSweetClickListener() {
@Override
public void onClick(SweetAlertDialog sweetAlertDialog) {
sweetAlertDialog.dismiss();
Intent intent = new Intent(getContext(), PaymentActivity.class);
intent.putExtra("amt", bill_in_paise.toString());
startActivityForResult(intent, REQUEST_CODE);
}
});
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
Toast.makeText(getContext(), error.toString(), Toast.LENGTH_LONG).show();
}// onErrorResnponse - END
});
// Add the request to the RequestQueue. Cuz volley is asyc af.
queue.add(endSessionRequest);
// VOLLEY REQUEST - END
}
});
endDialog.setButton(Dialog.BUTTON_NEGATIVE, "No", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(getContext(), "Session not cancelled. " + which, Toast.LENGTH_LONG).show();
}
});
endDialog.show();
}
}
}); // endSessionButton onclick - end
更新:@tuledev 通过变通方法帮助修复了 304,但问题仍然存在。即使状态码是 200,云函数也会以某种方式再次被调用,我得到一个 NaN 账单。目前我不知道是什么原因造成的。
【问题讨论】:
-
请编辑问题以准确解释客户端如何调用此函数。包括任何相关代码。
-
@DougStevenson 我已经用客户端代码更新了这个问题。对不起,我两天没上网了。
-
你用谷歌搜索过答案吗? stackoverflow.com/questions/50238968/…
-
你确定你的代码到达了
res.status(200)行吗? @ParamdeepSinghObheroi 我认为您的代码可能会在第一时间在某处引发异常。然后打不上res.status(200) -
@tuledev 所以经过一天的测试你的工作,它似乎工作。使用 bill 返回 endTime 不再导致错误。您可以将您的工作作为解决方案发布。如果我没有得到解决方案,我会在赏金结束时接受它。
标签: javascript node.js firebase firebase-realtime-database google-cloud-functions