我一直在尝试一个我正在致力于开发Food聊天机器人功能的项目。我目前正在做的是执行一种方法,供用户购买存储在Firebase实时数据库中的订单。
该方法被设置为actionMap的方法,并且actionMap链接到一个意图,该意图用于了解何时调用该方法以及获取参数。
我当前的方法是通过检查用户的参考路径并执行一个.forEach方法,对用户在数据库中的存在和状态进行简单检查,然后通过ID识别用户要购买的订单是否存在。检查找到的每个订单,并查看其父文件夹名称以检查其是否与用户的订单ID相匹配。我的代码如下:
const MakePurchaseACTION = 'Make Purchase';
function makePurchase(app){
let email = parameter.email;
let orderId = parameter.orderId;
var currDate = currDateGenerator();
var name = email.split(".com");
//Check if User exists first in database
var userRef = database.ref().child('user/' + name);
return userRef.once('value').then(function(snapshot) {
if (snapshot.exists()) {
let statusRetrieved = snapshot.child('Status').val();
//Check if user's status in database is signed in.
if (statusRetrieved == "Signed In") {
var orderRef = database.ref().child('order/' + name);
//Check the order table for the user.
return orderRef.once('value').then(function(orderSnapshot){
let orderVal = orderSnapshot.val();
console.log(orderVal);
//Check through every child for the matching id.
orderSnapshot.forEach(function(childSnapshot) {
let orderIdFound = childSnapshot.key;
//let cost = childSnapshot.child('Cost').val();
console.log(orderIdFound);
if(orderId == orderIdFound) {
let eateryName = childSnapshot.child('Eatery').val();
let eateryLocation = childSnapshot.child('EateryLocation').val();
let deliveryAddress = childSnapshot.child('DeliveryAddress').val();
let orderItem = childSnapshot.child('OrderItem').val();
let quantity = childSnapshot.child('Quantity').val();
let cost = childSnapshot.child('Cost').val();
var purchaseRef = database.ref().child('purchase/' + name + "/" + currDate + "/" + orderId);
purchaseRef.set({
"Eatery" : eateryName,
"EateryLocation" : eateryLocation,
"DeliveryAddress": deliveryAddress,
"OrderItem" : orderItem,
"Quantity": quantity,
"Cost": cost,
"DateCreated": currDate
});
app.add("You have successfully purchased Order " + orderId);
} else {
app.add("There is no order with that id.");
}
});
});
} else {
app.add("You need to be signed in before you can order!");
}
}
else {
app.add("Sorry pal you don't exist in the database.");
}
});
}
actionMap.set(MakePurchaseACTION, makePurchase);
在检查了一些Firebase日志后
Firebase Realtime Database Order Table Sample
我发现该方法实际上完成了Purchase table sample,但是我的对话流返回,并指出以下错误: 错误:未为平台定义任何响应:未定义,并向用户显示“不可用”。我的问题是如何解决这个错误?