kemsbe
kemsbe

Reputation: 605

Parse JS SDK: Cannot use the Master Key, it has not been provided

I need to use masterKey inside my angular2 app, but I can't pass it to initialize function and I can't google out why. From package.json: "parse": "~1.9.2".

Initialization:

import {Parse} from '~/node_modules/parse/dist/parse';

@Injectable()
export class TFCloudService {
    constructor() {
        this.parse = Parse;

        Parse.initialize(appConfig.parse.appId, null, appConfig.parse.masterKey);
        Parse.serverURL = appConfig.parse.clientServerUrl;
        Parse.liveQueryServerURL = appConfig.parse.liveQueryServerURL;
    }
}

Error source:

this.edittedUser.save(null, {useMasterKey: true})
.then((user) => {
    console.log(user);
});

Error text: Error: Cannot use the Master Key, it has not been provided.

appConfig.parse.masterKey works fine, I checked that line with hard-coded key too, but got the same result.

Upvotes: 3

Views: 3975

Answers (2)

saulsluz
saulsluz

Reputation: 92

By the book, you never should use the Master Key in the frontend.

So, the right way should be to create cloud functions for operations that require master key

const Parse = require('parse/node');
Parse.serverURL = 'https://parseapi.back4app.com'; // This is your Server URL
// Remember to inform BOTH the Back4App Application ID AND the JavaScript KEY
Parse.initialize(
  'APP_ID', // This is your Application ID
  'JS_KEY', // This is your Javascript key
  'MASTER_KEY' // This is your Master key (never use it in the frontend)
);

Parse.Cloud.define("hello", async (request) => {
    const query = new Parse.Query("SomeClass");
    const results = await query.distinct("field"); // needs Master Key
    // ...
});

Then, call it on browser apps

<script type="text/javascript" src="https://npmcdn.com/parse/dist/parse.min.js"></script>
<script type="text/javascript" type="text/javascript">
    Parse.initialize(
        "APP_ID",
        "JS_KEY"
    );
    Parse.serverURL = 'https://parseapi.back4app.com/'
    const helloFunction = await Parse.Cloud.run("hello");
</script>

Upvotes: 0

kemsbe
kemsbe

Reputation: 605

Actually guessed the right way to pass that key:

Parse.initialize(appConfig.parse.appId);
Parse.masterKey = appConfig.parse.masterKey;

Upvotes: 20

Related Questions