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);
});
});
我不能相信这是这么简单......我不知道,你可以限制一个领域,这是一个关系!谢谢! –