Skip to content

Cloud code function responds with error:undefined/unauthorized #546

Closed

Description

@paulfreeman

I have performed a test migration of my Parse db and application to a self-hosted solution.
I have successfully authenticated my PFUser with parse-server with an iOS client.

NB :
I have successfully executed the hello example in main.js using curl to ensure that my parse server endpoint is correctly configured. I can also execute this from my iOS client code. So cloud-functions are working on the desired end-point.

However I am having problems in getting any of my cloud-code Query functions to execute properly.

Here is an example of a test function that has the problem.

Parse.Cloud.define("galleryForUser", function(request, response) {
    var query = new Parse.Query("Gallery"); 
    query.equalTo("username", "testuser"); 
    query.useMasterKey = true; 
    query.find({ 
        success: function(results) { 
            response.success("Success"); 
        },
        error: function(err) { 
            console.log(err); 
            response.error("gallery lookup failed");  
        } 
    }); 
});

This returns an error and prints the following to the console.log(err) call in the parse-server stdout

ParseError { code: undefined, message: 'unauthorized' }

I get this error when calling from inside an iOS app, after authenticating and ensuring that PFUser is valid, or just when calling with curl outside of an established session.

It is unclear to me why the code should be undefined or what is happening here.

Is this a defect or is there something wrong with my cloud code?

I have added the query.userMasterKey = true line having read that this was required in the migration docs. Adding or removing it seems to make no difference.

Activity

  1. paulfreeman commented on Feb 21, 2016

    @paulfreeman
    Author

    The problem was with the Parse-Server initialisation in index.js

    The migration documents do not mention that its necessary to supply a value for 'serverURL' but unless you do then cloud-code calls won't work correctly.

    var api = new ParseServer({
      databaseURI: databaseUri || 'mongodb://localhost:27017/test',
      cloud: process.env.CLOUD_CODE_MAIN || __dirname + '/cloud/main.js',
      appId: process.env.APP_ID || 'myApp',
      masterKey: process.env.MASTER_KEY || 'myMaster',  //Add your master key here. Keep it secret!
      fileKey: 'myFiles',
      javascriptKey: 'myJSKey',
      clientKey:'myClientKey',
      **serverURL: 'http://localhost:1337/parse'**
    });
    
    
  2. Yoloabdo commented on Jul 7, 2016

    @Yoloabdo

    Hello @paulfreeman, where to add this var api? to main.js? or another file?

  3. paulfreeman commented on Jul 7, 2016

    @paulfreeman
    Author

    That goes wherever you initialise your Parse server. See the link in the README here [https://github.com/ParsePlatform/parse-server#parse-server--express]

  4. Yoloabdo commented on Jul 7, 2016

    @Yoloabdo

    I've seen that but couldn't figure it out too, i'm using nodechef and that's the tree of files i've

    -cloud/
      jobs.js
      main.js            Cloud Code functions, require ./app.js here
      app.js             Express app configuration and request handling logic
      -views/            View template files that Express needs to render
        hello.ejs
    -public/
      example.html       Static HTML files
      favicon.ico        Your favicon logo
      -stylesheets/      CSS stylesheets
        style.css
    
    

    i've tried to create app.js and insert into it but didn't work either, thanks a lot for the quick reply!

  5. paulfreeman commented on Jul 7, 2016

    @paulfreeman
    Author

    Yes in your case app.js is most likely the correct location where express is configured. YMMV, things may have changed since February when I posted the comment

  6. Yoloabdo commented on Jul 7, 2016

    @Yoloabdo

    here's my code, can you give it a look

    
    var express = require('express');
    var ParseServer = require('parse-server').ParseServer;
    
    var app = express();
    
    
    var api = new ParseServer({
      databaseURI: databaseUri || 'URi',
      cloud: process.env.CLOUD_CODE_MAIN || __dirname + '/cloud/main.js',
      appId: process.env.APP_ID || 'id',
      masterKey: process.env.MASTER_KEY || 'key',  //Add your master key here. Keep it secret!
      fileKey: 'key',
      javascriptKey: '',
      clientKey:'',
      **serverURL: 'URL'**
    });
    
    // Serve the Parse API at /parse URL prefix
    app.use('/parse', api);
    
    var port = 1337;
    app.listen(port, function() {
      console.log('parse-server-example running on port ' + port + '.');
    });
    
    

    anything wrong?

  7. rawatshailender commented on Nov 3, 2016

    @rawatshailender

    Please check the version of parse-server being used. I had found similar issues with 2.2.12

  8. flovilmart commented on Nov 3, 2016

    @flovilmart
    Contributor

    @Yoloabdo, you should contact nodechef support as they're not using the original parse-server

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions