parse-server icon indicating copy to clipboard operation
parse-server copied to clipboard

request.user is "undefined" in the "beforeSave" function in the Cloud code

Open grassland-curing-cfa opened this issue 10 years ago • 23 comments

In my ParseServer migrated into the Heroko + mLab stack,

I have a beforeSave function set for a custom "OBSERVATION" Class as below.

Parse.Cloud.beforeSave("OBSERVATION", function(request, response) {
    ... ...
    var currUser = request.user;
    console.log(request);
    console.log("*** User objectId: " + currUser.id + "; " + currUser.getSessionToken());
    ... ...
});

I want to get the user name triggering the Save event. According to the Parse JavaScript SDK Reference, a "BeforeSaveRequest" takes a "user" as a member, which represents the User triggering the "Save" event.

However, this is always "undefined" for me. Is there anything I missed or did wrong?

Steps to reproduce

Please include a detailed list of steps that reproduce the issue. Include curl commands when applicable.

  1. In the BeforeSave function, add console.log(request);

Expected Results

 { triggerName: 'beforeSave', 
   **user: {ParseObject {
    sessionToken: 'xxxxxxxxxxxxxxx',
    id: 'hDijlsmDP'
    }},**
   object:  
    ParseObject { 
      _objCount: 1538, 
      className: 'OBSERVATION', 
      id: 'xUskfmanDY' }, 
   master: true, 
   log: FileLoggerAdapter {}, 
   original:  
    ParseObject { 
      _objCount: 1541, 
      className: 'OBSERVATION', 
      id: 'xUskfmanDY' }
 } 

Actual Outcome

 { triggerName: 'beforeSave', 
   object:  
    ParseObject { 
      _objCount: 1538, 
      className: 'OBSERVATION', 
      id: 'xUskfmanDY' }, 
   master: true, 
   log: FileLoggerAdapter {}, 
   original:  
    ParseObject { 
      _objCount: 1541, 
      className: 'OBSERVATION', 
      id: 'xUskfmanDY' }
 } 

Environment Setup

  • Server
    • parse-server version (Be specific! Don't say 'latest'.) : 2.2.15
    • Operating System: Heroku Linux
    • Hardware: Heroku Dyno
    • Localhost or remote server? (AWS, Heroku, Azure, Digital Ocean, etc): Heroku
  • Database
    • MongoDB version: 3.0
    • Storage engine: Unknown
    • Hardware: Unknown
    • Localhost or remote server? (AWS, mLab, ObjectRocket, Digital Ocean, etc): mLab

Logs/Trace

You can turn on additional logging by configuring VERBOSE=1 in your environment.

grassland-curing-cfa avatar Oct 25 '16 03:10 grassland-curing-cfa

I see that master is set to true here, how did you save the object that this beforeSave should be called on? Is a user authenticated?

bohemima avatar Oct 25 '16 05:10 bohemima

I see the same behaviour in afterSave cloud functions. request.user is always undefined

florianbepunkt avatar Oct 25 '16 13:10 florianbepunkt

Please answer all my questions. How is the object saved? Is there a currently authenticated user at all?

bohemima avatar Oct 25 '16 13:10 bohemima

I see my error. Was using it when user was signing up. thanks.

florianbepunkt avatar Oct 25 '16 14:10 florianbepunkt

@bohemima I used "Parse.Cloud.useMasterKey();" in the beforeSave function. Did this make the object saved correctly? Also the "OBSERVATION" class has both the Public Access and Write.

grassland-curing-cfa avatar Oct 25 '16 22:10 grassland-curing-cfa

Why was this issue closed? Has it already been solved? Where is the solution please?

grassland-curing-cfa avatar Oct 25 '16 22:10 grassland-curing-cfa

Can you provide a curl example of the request you're sending to your server?

Parse.Cloud.useMasterKey() is not available in Parse Server.

hramos avatar Oct 26 '16 23:10 hramos

@hramos I am not sure how to generate a curl command as it was from the beforeSave function. Isn't it automatically triggered by the Parse Server before a new document is inserted to the table? As Parse.Cloud.useMasterKey() is not available in Parse Server, can I get rid of this line from the code?

grassland-curing-cfa avatar Oct 27 '16 00:10 grassland-curing-cfa

Can you provide the logs when running with VERBOSE=1?

flovilmart avatar Oct 27 '16 01:10 flovilmart

@flovilmart Where should I set VERBOSE = 1? In Heroku or ParseServer? My ParseServer was deployed to Heroku from the GitHub repository ParseServerExample.

grassland-curing-cfa avatar Oct 27 '16 01:10 grassland-curing-cfa

In the heroku environment variables.

flovilmart avatar Oct 27 '16 03:10 flovilmart

I added VERBOSE=1 to the config. vars in Heroku settings; and added a new record to the OBSERVATION table with only APP-ID and MASTER-KEY set in the curl headers.

The Heroku console printed:

Oct 27 16:39:08 nemp-nsw-dev heroku/router: at=info method=POST path="/parse/classes/OBSERVATION" host=nemp-nsw-dev.herokuapp.com request_id=36c9b811-cdb0-4599-99d9-0d3feb79429b fwd="107.178.194.77" dyno=web.1 connect=2ms service=125ms status=500 bytes=546 Oct 27 16:39:09 nemp-nsw-dev app/web.1: { triggerName: 'beforeSave', Oct 27 16:39:09 nemp-nsw-dev app/web.1: object: ParseObject { _objCount: 731, className: 'OBSERVATION' }, Oct 27 16:39:09 nemp-nsw-dev app/web.1: master: true, Oct 27 16:39:09 nemp-nsw-dev app/web.1: log: FileLoggerAdapter {} } Oct 27 16:39:09 nemp-nsw-dev app/web.1: verbose: error: TypeError: Cannot read property 'id' of undefined Oct 27 16:39:09 nemp-nsw-dev app/web.1: at /app/cloud/main.js:444:49 Oct 27 16:39:09 nemp-nsw-dev app/web.1: at /app/node_modules/parse-server/lib/triggers.js:202:5 Oct 27 16:39:09 nemp-nsw-dev app/web.1: at new Promise (/app/node_modules/babel-polyfill/node_modules/core-js/modules/es6.promise.js:193:7) Oct 27 16:39:09 nemp-nsw-dev app/web.1: at Object.maybeRunTrigger (/app/node_modules/parse-server/lib/triggers.js:193:10) Oct 27 16:39:09 nemp-nsw-dev app/web.1: at /app/node_modules/parse-server/lib/RestWrite.js:186:21 Oct 27 16:39:09 nemp-nsw-dev app/web.1: at run (/app/node_modules/babel-polyfill/node_modules/core-js/modules/es6.promise.js:89:22) Oct 27 16:39:09 nemp-nsw-dev app/web.1: at /app/node_modules/babel-polyfill/node_modules/core-js/modules/es6.promise.js:102:28 Oct 27 16:39:09 nemp-nsw-dev app/web.1: at flush (/app/node_modules/babel-polyfill/node_modules/core-js/modules/_microtask.js:18:9) Oct 27 16:39:09 nemp-nsw-dev app/web.1: at _combinedTickCallback (internal/process/next_tick.js:67:7) Oct 27 16:39:09 nemp-nsw-dev app/web.1: at process._tickDomainCallback (internal/process/next_tick.js:122:9) Oct 27 16:39:09 nemp-nsw-dev app/web.1: error: Uncaught internal server error. [TypeError: Cannot read property 'id' of undefined] TypeError: Cannot read property 'id' of undefined Oct 27 16:39:09 nemp-nsw-dev app/web.1: at /app/cloud/main.js:444:49 Oct 27 16:39:09 nemp-nsw-dev app/web.1: at /app/node_modules/parse-server/lib/triggers.js:202:5 Oct 27 16:39:09 nemp-nsw-dev app/web.1: at new Promise (/app/node_modules/babel-polyfill/node_modules/core-js/modules/es6.promise.js:193:7) Oct 27 16:39:09 nemp-nsw-dev app/web.1: at Object.maybeRunTrigger (/app/node_modules/parse-server/lib/triggers.js:193:10) Oct 27 16:39:09 nemp-nsw-dev app/web.1: at /app/node_modules/parse-server/lib/RestWrite.js:186:21 Oct 27 16:39:09 nemp-nsw-dev app/web.1: at run (/app/node_modules/babel-polyfill/node_modules/core-js/modules/es6.promise.js:89:22) Oct 27 16:39:09 nemp-nsw-dev app/web.1: at /app/node_modules/babel-polyfill/node_modules/core-js/modules/es6.promise.js:102:28 Oct 27 16:39:09 nemp-nsw-dev app/web.1: at flush (/app/node_modules/babel-polyfill/node_modules/core-js/modules/_microtask.js:18:9) Oct 27 16:39:09 nemp-nsw-dev app/web.1: at _combinedTickCallback (internal/process/next_tick.js:67:7) Oct 27 16:39:09 nemp-nsw-dev app/web.1: at process._tickDomainCallback (internal/process/next_tick.js:122:9)

grassland-curing-cfa avatar Oct 27 '16 05:10 grassland-curing-cfa

@grassland-curing-cfa a curl request that creates the object that is covered by this beforeSave hook would suffice. The curl request should show that the object is in fact being saved as part of an authenticated user session.

hramos avatar Oct 27 '16 06:10 hramos

The curl request is:

curl -X POST -H "X-Parse-Application-Id: {APP_ID}" -H "X-Parse-Master-Key: {MASTER_KEY}" -H "Content-Type: application/json" -d "{\"ObservationStatus\":0, \"AreaCuring\":20,\"Comments\":\"Entered to Parse-server-example API on Heroku\"}" https://nemp-nsw-dev.herokuapp.com/parse/classes/OBSERVATION

grassland-curing-cfa avatar Oct 27 '16 06:10 grassland-curing-cfa

As I've been saying from the start, there is no user authenticated here (No Session-Token sent) and you are using the Master-Key.

bohemima avatar Oct 27 '16 07:10 bohemima

as @bohemima said, the request.user is only set if you pass a valid session token to your request.

flovilmart avatar Oct 27 '16 13:10 flovilmart

If I pass in a valid session-token to creating an object, does the beforeSave function picks up the user?

grassland-curing-cfa avatar Oct 27 '16 22:10 grassland-curing-cfa

Yes, that's pretty much the only way

flovilmart avatar Oct 27 '16 23:10 flovilmart

Thanks to all for your help! @flovilmart @bohemima @hramos I should have used REST-API-KEY and SESSION-TOKEN.

grassland-curing-cfa avatar Oct 27 '16 23:10 grassland-curing-cfa

wait....:) the request.object in beforeSave is not the object as it used to be in parse hosted this is my log when asking for:request.object: ParseObject {_objCount: 133, className: "Address"}

here is my cloud code: //this function put admins role in address of users Parse.Cloud.beforeSave("Address", function(request, response) { var oldAcl = request.object.get('ACL'); var addressACL = oldAcl; addressACL.setRoleReadAccess("admins", true); request.object.setACL(addressACL); response.success(); });

what am i missing here?

this is the log when i ask for request: Object {triggerName: "beforeSave", object: ParseObject, master: false …} installationId:"93569ef0-8acd-4d33-9fb8-d2ce43ecc051" log:LoggerController {options: undefined, appId: "kAnTpwWcbY1oYOzvvsOcp5E1mvS8J8IFttaM6qbp", undefined: WinstonLoggerAdapter} master:false object:ParseObject {_objCount: 133, className: "Address"} triggerName:"beforeSave" user:ParseUser {_objCount: 122, className: "_User", id: "L7qdo0iCzk"} proto:Object {constructor: function Object() { [native code] }, toString: function toString() { [native code] }, toLocaleString: function toLocaleString() { [native code] } …}

kalitas avatar Nov 09 '16 11:11 kalitas