Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
paulfreeman opened this issue Feb 21, 2016 · 8 comments
Closed

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

paulfreeman opened this issue Feb 21, 2016 · 8 comments

Comments

@paulfreeman
Copy link

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.

@paulfreeman
Copy link
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'**
});

@Yoloabdo
Copy link

Yoloabdo commented Jul 7, 2016

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

@paulfreeman
Copy link
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]

@Yoloabdo
Copy link

Yoloabdo commented Jul 7, 2016

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!

@paulfreeman
Copy link
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

@Yoloabdo
Copy link

Yoloabdo commented Jul 7, 2016

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?

@rawatshailender
Copy link

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

@flovilmart
Copy link
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
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants