I need to change the value of the array from ‘User’ to ‘Admin’ if the function is clicked on and I have to code it in cloud code.
but there is a problem the array does not change
the following code is working but only the part with nameRoleQuery is not working and that’s the part which I need to change.
promoteToAdmin: function promoteToAdmin(request, response) { if (!request.params.companyUser) { response.error('Request did not have an authenticated user attached with it'); } else { var companyUser; var companyUserQuery = new Parse.Query('CompanyUser'); companyUserQuery.include('user'); companyUserQuery.include('company'); companyUserQuery.get(request.params.companyUser, { useMasterKey: true }).then((giveRolename) => { var nameRoleQuery = new Parse.Query(Parse.Role); request.nameRoleQuery.set('user', ['Admin']); return nameRoleQuery.save(null, { useMasterKey: true }); }).then((companyUserObject) => { companyUser = companyUserObject; var userRoleQuery = new Parse.Query(Parse.Role); userRoleQuery.equalTo('name', 'Company-User-' + companyUser.get('company').id); return userRoleQuery.first({ useMasterKey: true }); }).then((userRole) => { var usersInUserRole = userRole.relation('users'); usersInUserRole.remove(companyUser.get('user')); return userRole.save(null, { useMasterKey: true }); }).then((userRoleSaveResult) => { var adminRoleQuery = new Parse.Query(Parse.Role); adminRoleQuery.equalTo('name', 'Company-Admin-' + companyUser.get('company').id); return adminRoleQuery.first({ useMasterKey: true }); }).then((adminRole) => { var usersInAdminRole = adminRole.relation('users'); usersInAdminRole.add(companyUser.get('user')); return adminRole.save(null,{ useMasterKey: true }); }).then((saveResult) => { console.log('after'); response.success('fissa is aan'); }, (error) => { console.log(error); }); console.log(); } }
the role array needs to change.
Advertisement
Answer
Still unsure exactly what you’re trying to do, but this is what I imagine you want to happen.
CompanyUser
has a pointer called ‘user’ of type Parse.User
. You want to update this Parse.User
, based on your image, when you run this query. The following should work:
//AS ABOVE .then((giveRolename) => { var user = giveRoleName.get("user"); user.remove('role','user'); user.add('role','admin'); return user.save(null, {useMasterKey:true}); }).then((companyUserObject) => { //companyUserObject is Parse.User object. If CompanyUser object is needed, store in variable beforehand.
I’ve put below what I would suggest as an improvement to your current code. It’s less sequential and provides greater fallback in an error happens in the middle of the process. It’s without arrow functions, so you’ll have to change accordingly.
function promoteToAdmin(request, response) { var params = request.params; var companyUserId = params["companyUser"]; var companyUser; return Parse.Promise.as().then( function() { if(!companyUser) { return Parse.Promise.error("Request did not have an user attached with it."); } var CompanyUser = Parse.Object.extend("CompanyUser"); var companyUserQuery = new Parse.Query(CompanyUser); companyUserQuery.include("user"); companyUserQuery.include("company"); return companyUserQuery.get(companyUserId,{useMasterKey:true}); } ).then( function(fetchedCompanyUser) { companyUser = fetchedCompanyUser; var company = companyUser.get("company"); var userRoleQuery = new Parse.Query(Parse.Role); userRoleQuery.equalTo('name', "Company-User-" + company.id ) var adminRoleQuery = new Parse.Query(Parse.Role); adminRoleQuery.equalTo('name', "Company-Admin-" + company.id); return Parse.Promise.when( userRoleQuery.first({useMasterKey:true}), adminRoleQuery.first({useMasterKey:true}) ) } ).then( function(userRole,adminRole) { if(!userRole || !adminRole) { return Parse.Promise.error("No role found") } var user = companyUser.get("user"); user.remove("role","user"); user.add("role","admin"); userRole.getUsers().remove(user); adminRole.getUsers().add(user); return Parse.Promise.when( user.save(null,{useMasterKey:true}), userRole.save(null,{useMasterKey:true}), adminRole.save(null,{useMasterKey:true}) ) } ).then( function(user,userRole,adminRole) { response.success("success"); }, function(error) { response.error(error); } ) }