Start a new topic
Answered

Bug(?): User attributes deleted after update

Hello 

I'm using REST API of Kinvey and doing all processing with PHP (CURL). As far as I understand the related documentation I can update specific columns in the USER collection. 

If I f.e. update the USERNAME or PASSWORD any additional previously created attributes will be emptied. Here is the PHP code I'm using: 


$ch = curl_init(); 

curl_setopt ($ch, CURLOPT_URL, 'https://baas.kinvey.com/user/kid_SkYzM4SHM/'.$cloudid); // $cloudid represents the affected _ID 

curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "PUT"); 

curl_setopt ($ch, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_1); 

curl_setopt($ch, CURLOPT_HTTPHEADER, array("Content-Type: application/json","Authorization: Kinvey ".$usertoken, "X-Kinvey-API-Version: 3" )); 

curl_setopt ($ch, CURLOPT_POSTFIELDS, json_encode($change) ); // $change: see below 

curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); 


$result = json_decode(curl_exec ($ch), true); curl_close ($ch); 


=> $cloud f.e. in case of changing password: {"password":"22222"} 


Output of $result after update: 


Array ( [_id] => 5afb5012928fd8471a6066b1 

[username] => testuser [_acl] => Array ( 

[creator] => 5afb5012928fd8471a6066b1 ) 

[_kmd] => Array ( [lmt] => 2018-05-15T21:25:59.313Z [ect] => 2018-05-15T21:24:34.248Z 

[authtoken] => 71ccd824-199d-40a1-9c59-5882c2cb7616.N6FLINAyGQZH+5D+rhrHHLzKBVy9Wp9XWtdLI7q4tZo= )

 ) 


-> Looks all fine to me. The password has also changed correctly (tested). 

I'm also aware that the authtoken has changed. See attachment before and after update. 

As you can see the attributes "langauge" and "friendaccept" are empty after update. 


Regards  


Best Answer

Tayger,


The behavior that you are seeing is actually an expected behavior. If you refer the documentation provided on this link, it clearly mentions "Currently there is no way to pass only the attributes you want to be changed. The backend stores the entire JSON body as passed in the request body.". For updating an entity you have to send the whole entity along with the new fields (In your case - 'language' and 'friendaccept' fields) that you want to update.


Let me know if anything is unclear.



Thanks,

Pranav

Kinvey


Answer

Tayger,


The behavior that you are seeing is actually an expected behavior. If you refer the documentation provided on this link, it clearly mentions "Currently there is no way to pass only the attributes you want to be changed. The backend stores the entire JSON body as passed in the request body.". For updating an entity you have to send the whole entity along with the new fields (In your case - 'language' and 'friendaccept' fields) that you want to update.


Let me know if anything is unclear.



Thanks,

Pranav

Kinvey

Hi Pranav


Thank you to make that clear. I'm aware of this behaviour and acting that way. I thought it's different for the USER collection: here

The update part mentions to send some specific update attributes but that seems to be for new attributes only. 


All fine, so I change my code to send all required attributes.


Makes me thinking if its not faster to delete the affected record before inserting a new one instead of updating. I can check that out.


Thank you!

You can use the $set operator from a Custom Endpoint like so:  

var access_code_id = modules.collectionAccess.objectID(request.body.access_code_id);

var kinvey_ElevateAccounts = modules.collectionAccess.collection("ElevateAccounts");

var data_obj = {
	$set: {
		"Activated": true,
	}
};

kinvey_ElevateAccounts.update({ "_id": access_code_id }, data_obj, function (err, result) {
	if (err) {
		response.body = { message: "Query failed: " + err, error: 106 };
		response.complete(400); // Bad Request
	}
	else {
		if (debug) logger.info("result = " + JSON.stringify(result));
		response.complete(200); // OK
	}
});

   

Login or Signup to post a comment