什么,SO?
我有一个运行一些查询的云代码功能,以确保在当前时间允许我想要做的事情。如果这些查询告诉我它不被允许,我会调用response.error()并解释原因,并且我有一个else语句,它返回另一个承诺来运行新查询或执行其他操作。
我发现即使我调用了response.error()并且没有返回一个承诺,它也会跳转到链中的下一个链接,这会导致一些未定义的行为,并且非常有问题地修改我不想修改的内容。
我唯一的选择是设置一个标志并在链条的每一步检查它吗?我觉得必须有一个更好的方法来提前退出链条。
感谢您的任何提示/反馈,我真的很感激。
编辑 - 以下是此问题发生的示例。我使用Stripe作为我的支付平台,并希望用户能够删除他们的卡。由于我将Stripe Connect Market与托管帐户一起使用,因此我必须根据提供商操作而不是用户请求时间来创建/捕获费用,因此我会存储他们选择的卡令牌。如果我仍然需要它来向客户收费,我需要确保该卡不会从其帐户中删除。目前,如果用户有任何有效/预定的服务,我根本不允许用户删除卡,但我已经让用户抱怨过,所以我想更好地实现删除功能。
Parse.Cloud.define("StripeDeleteCard", function(request, response)
{
//Parse objects
var query1 = new Parse.Query("Class1");
var query2 = new Parse.Query("Class2");
//passed in parameters
var customer_id = request.params.customer_id;
var card_id = request.params.card_id;
//Set up queries to ensure the card is not being used on an instance of Class1 or Class2
query1.equalTo("customerCard", card_id);
query2.equalTo("customerCard", card_id);
query1.count().then
(
function( number )
{
if( number > 0 )
{
response.error("CARD CAN NOT BE DELETED BECAUSE IT IS BEING USED AS THE PAYMENT METHOD FOR AN ACTIVE SERVICE REQUEST!"); //This is where I want my function to end
}
else
{
return query2.count(); //This isn't actually getting called, so "number" is nil in the next link
}
},
function( error )
{
response.error("Failed to query for Class1 using this card_id: " + error.message);
}
).then //Everything after this shouldn't execute
(
function( number ) //number is not set since no promise was returned
{
if( number > 0 )
{
response.error("CARD CAN NOT BE DELETED BECAUSE IT IS BEING USED AS THE PAYMENT METHOD FOR A SCHEDULED SERVICE REQUEST!");
}
else //This block gets called anyway, since number == 0
{
return Parse.Cloud.httpRequest(
{
method:"DELETE",
url: "https://" + STRIPE_SECRET_KEY + ':@' + STRIPE_API_BASE_URL + "/customers/" + customer_id + "/cards/" + card_id
}
);
}
},
function( error )
{
response.error("Failed to query for Class2 using this card_id: " + error.message);
}
).then
(
function( httpResponse )
{
response.success("Card Deleted."); //I just deleted a card that should not have been deleted
},
function( error )
{
response.error("Failed to delet the card: " + error.message);
}
);
});