1
我有一個承諾鏈執行查詢,檢查結果,如果我找到我需要它調用response.success(),如果不,它移動到下一個查詢...由於某種原因,但是當我調用response.success()時,下一個promise會被執行。不response.success()結束函數?Javascript/Parse.com - 承諾,調用response.success()承諾
此代碼實質上檢查我的每個角色,以查看請求用戶屬於哪一個。
當我調用response.success()時,如何停止承諾鏈?
Parse.Cloud.define('getUserRole', function (request, response) {
Parse.Cloud.useMasterKey();
const NAME = "name";
const USER = "user";
const USERNAME = "username";
const CUSTOMER_ROLE = "Customer";
const BARBER_ROLE = "Barber";
const ADMIN_ROLE = "Admin";
var user = request[USER];
if (user == null){
console.log("@Null Check for user");
response.success(CUSTOMER_ROLE);
return;
} else {
console.log("Username: " + user.get(USERNAME));
}
//Query customers first because that is the most likely role for most cases
var customerRoleQuery = new Parse.Query(Parse.Role);
customerRoleQuery.equalTo(NAME, CUSTOMER_ROLE);
customerRoleQuery.first().then(function (role) {
console.log("@query customer Role: " + JSON.stringify(role));
//Customer Role
var customerQuery = role.getUsers().query();
customerQuery.equalTo(USERNAME, user.get(USERNAME));
return customerQuery.first();
}).then(function (user) {
console.log("@query for customer users: " + JSON.stringify(user));
//Check if we found the user in the customer role
if (user){
console.log("@ customer user");
//We found the user in the customer role
response.success(CUSTOMER_ROLE);
} else {
console.log("@ barber role query");
//We didn't find the user in the customer role, get the barber role.
var barberRoleQuery = new Parse.Query(Parse.Role);
barberRoleQuery.equalTo(NAME, BARBER_ROLE);
return barberRoleQuery.first();
}
}).then(function (role) {
console.log("@query Barber Role: " + JSON.stringify(role));
//Barber Role
var barberQuery = role.getUsers().query();
barberQuery.equalTo(USERNAME, user.get(USERNAME));
return barberQuery.first();
}).then(function (user) {
console.log("@query for barber users: " + JSON.stringify(user));
//Check if we found the user in the barber role
if (user){
//We found the user in the barber role
response.success(BARBER_ROLE);
} else {
//We didn't find the user in the barber role, get the admin role
//This may seem unnecessary, but we can't assume if we didn't find them in the customer or barber roles they are admins. That is a security risk.
var adminRoleQuery = new Parse.Query(Parse.Role);
adminRoleQuery.equalTo(NAME, ADMIN_ROLE);
return adminRoleQuery.first();
}
}).then(function (role) {
console.log("@query Admin Role: " + JSON.stringify(role));
//Admin Role
var adminQuery = role.getUsers().query();
adminQuery.equalTo(USERNAME, user.get(USERNAME));
return adminQuery.first();
}).then(function (user) {
console.log("@query admin users: " + JSON.stringify(user));
//Check if we found the user in the admin role
if (user){
//We found the user in the admin role
response.success(ADMIN_ROLE);
} else {
//Still didn't find the user in a role, just assume they are customers
response.success(CUSTOMER_ROLE);
}
}, function(error){
console.log("@Error");
response.error(error);
});
});
我不能相信這是這麼簡單......我不知道,你可以限制一個領域,這是一個關係!謝謝! –