Parse.com中的预定作业

时间:2019-05-18 14:25:56

标签: javascript node.js parse-platform job-scheduling cloud-code

我想在Parse中使用计划的作业运行Cloud Code。(sashido.io)

randomMentor 函数以随机数选择Class中的某个对象并将其保存为Today类。当我通过PF.Cloud.callFunctionInBackground从Swift调用此方法时,它有效很好。

但是我无法从 Parse Dashboard 中进行工作。我在仪表板中创建了一个名为pickTodaysMentor的计划作业,该作业尝试使用Parse.cloud.run关闭运行。 但它总是返回: error loading cloud code Parse.Cloud.define is not a function

有我的functions.js;

Parse.Cloud.define('randomMentor', async function (request, response) {
    var Mentor = Parse.Object.extend('Mentor');
    var countQuery = new Parse.Query(Mentor);
    const count = await countQuery.count();
    const query = new Parse.Query('Mentor');
    const randomInt = Math.floor(Math.random() * count);
    query.equalTo('position', randomInt);
    query.limit(1); // limit to at most 10 results
    const results = await query.find();

    const Today = Parse.Object.extend('Today');
    const today = new Today();
    today.set('mentor', results[0]);
    today.save()
        .then((today) => {
            response.success("Today Mentor Created.");
        }, (error) => {
            response.error(error);
        });
    response.success("Today Mentor Created.");
});

Parse.Cloud.job('pickTodaysMentor', (request) => {
    const { params, headers, log, message } = request;
    message("I just started");
    Parse.Cloud.run('randomMentor', {}, {
        success: function (result) {
        },
        error: function (error) {
        }
    });
});

我不是JS开发人员,所以找不到任何解决方案。我挖了教程,指南和谷歌,但没有运气。我认为错误对于发现问题过于笼统。

您如何看待?

非常感谢您的编码...

1 个答案:

答案 0 :(得分:0)

The error you are receiving means that your Cloud Code function is invalid.

It should look something like this in versions of Parse Server 3.0.0 and above:

Parse.Cloud.define("function", async (request) => {
  return "Hello World";
});