Start a new topic

Update another user's details

Hi


Anyone know how can you update the details of another user. I have set the _acl to allow a the active user read write access to the other user's profile. I have been looking around and cannot find any examples or references on how to update a user other than the active user. All my attempts seem to target the active user only


Use case: Admin user managing team of users. 


This article suggests it is possible if the _acl is set.

https://support.kinvey.com/support/discussions/topics/5000041722


Using v3.3.3


Cheers


Hi,


Can you tell me which Permissions you have selected in Users>Settings in Kinvey Console?


Another way to achieve this is by using BL Custom endpoints. I think that will be a better fit for your use case.



Regards,

Wani

Hey there Wani


Users>Settings>Permissions = "Shared"


_acl = {

  "groups": {

    "r": [

      "585c8d803da2ab16132ef6a9_admin"

    ],

    "w": [

      "585c8d803da2ab16132ef6a9_admin"

    ]

  },

  "creator": "kid_XXXXXXX"

}


My issue is I am not sure what code I am using to target and modify a user. Happy to use BL endpoint but how do I modify a user? My understanding is CollectionAccess is different for Users? Could not find User modules in BL reference. Sorry if I missed it :)

Hi Phil,


Sorry about the late reply. You can just use modules.collectionAccess.collection('user') to access user collection.


Do try this and let me know how it goes.



Regards,

Wani

Ok I try not to ask for help when its my lack of knowledge but I can't for the life of me get the mongo update to work. Where am I wrong?    

function onRequest(request, response, modules) {
var user_obj = request.body;
  	
  modules.logger.info(user_obj._id);
    
  modules.collectionAccess.collection('user').update({_id: modules.collectionAccess.objectID(user_obj._id)}, {$set:user_obj}, {w: 1}, function(err,user){
    
      if(err){
        modules.logger.info(err);
        response.complete(400);	
      }
     modules.logger.info(user);
     response.body = user
      response.complete(200);
    });

}

   The object it receives 

{_id:"5861b10522208910005ba0f1"
email:"john.smith@smith.com"
first_name:"John"
last_name:"Smith"}

 Result: "Error: An error has occurred within MongoDB while trying to execute your query"

Hi,


As per the documentation(http://devcenter.kinvey.com/rest/reference/business-logic/reference.html#collection-access-module), the update method has following parameters: update(query, doc, options, callback)


A couple of suggestions to try out:

  • In the second parameter, send user_obj directly instead of {$set:user_obj}
  • Add return statement with response.complete
  • Make sure you are sending the complete object in user_obj with updated as well as non-updated column-value-pairs


Regards,
Wani

Thank you Wani. I will go back and have a look at this one :) Busy couple weeks. 

Hi Wani


This one did my head in. 


I finally found a solution. I had to remove _id from the object that I was updating. I am a mongo novice so feel free to correct me for future readers. 


Here is the code I finally got it working with: 

function onRequest(request, response, modules) {

var userGroupName = "user";
var user_obj = request.body
var user_id = request.body._id

delete user_obj._id
    
    modules.collectionAccess.collection(userGroupName).update({_id: modules.collectionAccess.objectID(user_id)}, {$set:user_obj},  function(err,result){
      if(err){
        modules.logger.error(err);
        response.body=err
        response.complete(400);	
      }else{
        modules.logger.info(result);
        response.complete(200);
      }
    });

}

  and the object passed in: 

{_id:"5861b10522208910005ba0f1"
email:"john.smith@smith.com"
first_name:"John"
last_name:"Smith"}

 

It's now updating the user entity as intended. Thanks for the help.

Hi Guys


Any reason I am getting intermittent BL time outs on this code? About 1 in 20 will timeout but there is no way this is hitting 2 seconds?


 

function onRequest(request, response, modules) {

var userGroupName = "user";
var user_obj = request.body
var user_id = request.body._id

delete user_obj._id
    
    modules.collectionAccess.collection(userGroupName).update({_id: modules.collectionAccess.objectID(user_id)}, {$set:user_obj},  function(err,result){
      if(err){
        modules.logger.error(err);
        response.body=err
        response.complete(400);	
      }else{
        modules.logger.info(result);
        response.complete(200);
      }
    });

}

 I have put a retry into the code with a 300ms timeout. As you can see here fails twice before thenb succeeding. Nothing changes in that time and there is only a single .update call to the collection in the BL. 


I called that endpoint successfully 35 times before it failed. It then failed twice and worked again. 


image

 

Here is the error message:


ServerErrorcode: 500debug: "The execution of a background task timed out."kinveyRequestId: undefinedmessage: "The Business Logic script did not complete. Please contact support"name: "BLTimeoutError"stack: "Error: The Business Logic script did not complete. Please contact support↵ at ServerError.BaseError (https://da189i1jfloii.cloudfront.net/js/kinvey-html5-sdk-3.3.5.js:2508:20)↵ at new ServerError (https://da189i1jfloii.cloudfront.net/js/kinvey-html5-sdk-3.3.5.js:3367:108)↵ at KinveyResponse.get (https://da189i1jfloii.cloudfront.net/js/kinvey-html5-sdk-3.3.5.js:5968:18)↵ at get (https://da189i1jfloii.cloudfront.net/js/kinvey-html5-sdk-3.3.5.js:5875:473)↵ at KinveyResponse.get (https://da189i1jfloii.cloudfront.net/js/kinvey-html5-sdk-3.3.5.js:6037:17)↵ at https://da189i1jfloii.cloudfront.net/js/kinvey-html5-sdk-3.3.5.js:22006:26↵ at tryCatch (https://da189i1jfloii.cloudfront.net/js/kinvey-html5-sdk-3.3.5.js:4797:13)↵ at invokeCallback (https://da189i1jfloii.cloudfront.net/js/kinvey-html5-sdk-3.3.5.js:4812:14)↵ at publish (https://da189i1jfloii.cloudfront.net/js/kinvey-html5-sdk-3.3.5.js:4780:8)↵ at MutationObserver.flush (https://da189i1jfloii.cloudfront.net/js/kinvey-html5-sdk-3.3.5.js:4508:6)"__proto__: BaseError

onError @ team.js:637

tryCatch @ kinvey-html5-sdk-3.3.5.js:4797

invokeCallback @ kinvey-html5-sdk-3.3.5.js:4812

publish @ kinvey-html5-sdk-3.3.5.js:4780

publishRejection @ kinvey-html5-sdk-3.3.5.js:4722

flush @ kinvey-html5-sdk-3.3.5.js:4508



Hello Phil,


I investigated the BLTimeoutError issues that you reported and they didn't look right so after engaging engineering we determined that this was a temporary business logic capacity problem that we experienced for about an hour during the period that you experienced the timeout errors. This capacity problem has been resolved and you should no longer see these timeouts.


Your app shows no further BL timeout errors after 16:21:05 Central Standard Time today.


I hope this helps. Please let me know if you have any questions.


Regards,


Billy Gee

Billy Gee


Thank you for that my friend. Thanks for jumping on the BL performance so fast. 


App is in Dev so I haven't done any calls since them but great to know there was a load issue and not poor code.


Thanks guys. 


Phil

Login or Signup to post a comment