Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Background job using parse-server & Heroku Scheduler

Using parse-server and heroku for my Android application. Want to create background jobs, like the ones offered by Parse.com earlier, but can't seem to make it work. In the parser-server-example folder, I have added a jobs.js file containing this:

var Parse = require('parse/node');
Parse.initialize('my_app_id');
Parse.serverURL = 'https://random-name.herokuapp.com/parse';

function saveSomething(){
var obj = new Parse.Object('my_class', 'random');
obj.save(null, {
    success: function(place){
        console.log("Success!!");
    },
    error: function(place, error){
        console.log("Fail: " + error.message);
    }
});
}

function sayHello() {
console.log('Hello');
}

sayHello();
saveSomething();

sayHello() runs fine, but saveSomething() get "error: unauthorized" message when i run: heroku run node jobs.js . So I have 2 questions.

1.Is this det correct way to create a background job using parse-server & heroku?

2.Anything wrong with the "jobs.js" code/something that should be done differently in general to accomplish the background job task?

(Tried adding javascript-key to the Parse.initialize('app-id', 'javascript-key'); without any luck)

like image 939
Slagathor Avatar asked Dec 01 '25 02:12

Slagathor


1 Answers

The solutions was to add "Parse.Cloud.useMasterKey();" after the line "Parse.serverURL = 'https://random-name.herokuapp.com/parse', and add the apps master key in the "Parse.initialize('my_app_id', 'js-key', 'master-key'); to get the correct authorisation.

var Parse = require('parse/node');
Parse.initialize('app-id', 'js-key','master-key');
Parse.serverURL = 'https://random-name.herokuapp.com/parse/';
Parse.Cloud.useMasterKey();

function saveSomething(){
var MyClass = Parse.Object.extend("MyClass");
var myclass = new MyClass();
myclass.set("columnName", "value");
myclass.save({
    success: function(place){
        console.log("Success!!");
    },
    error: function(place, error){
        console.log("Fail: " + error.message);
    }
});
}

saveSomething();

Hope this helps someone.

like image 63
Slagathor Avatar answered Dec 02 '25 17:12

Slagathor