我有一个执行查询的promise链,检查结果,如果我找到了我需要的东西,则调用response.success(),如果没有,它会移动到下一个查询...由于某种原因,但是当我调用response.success()来执行下一个promise。没有response.success()结束函数吗?
此代码实质上检查我的每个角色,以查看请求用户所属的角色。
当我调用response.success()时,如何停止promise链?
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);
});
});
答案 0 :(得分:1)
response.success()
终止任何正在进行的异步调用,但它本身并不终止云代码。我认为发布的代码中发生的是success()
被调用,然后当前承诺的then
运行(这会影响您的日志,但没有其他内容)。
我们可以做一些事情来更好地组织当前方法下的代码,但最好还是完全改变方法。无需在代码中枚举角色以查看用户具有的角色。考虑一下:
// return a promise that's fulfilled with any Parse.Role which has user in the 'users' relation
function getUserRoles(user) {
var query = new Parse.Query(Parse.Role);
query.equalTo('users', user);
return query.find();
}
为云代码包装也很容易:
Parse.Cloud.define('getUserRole', function(request, response) {
getUserRoles(request.user).then(function(roles) {
response.success(roles);
}, function(error) {
response.error(error);
});
});