解析计划的后台作业

时间:2019-05-14 07:36:42

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

我正在尝试制作一个具有每日报价逻辑并显示报价的应用程序。它应该在我的random object中选择parse class并将其显示给用户。如果用户看到了今天的对象,他们应该不会在同一天看到其他随机对象。

我用Swift制作了此算法。但是我认为Cloud CodeBackground Job是执行此算法的更清晰正确的方法。我研究了background job教程指南等,但是却做不到,因为我没有足够的JavaScript知识来做到这一点。像这样在Background Job中创建Parse server的一切;

    Parse.Cloud.define('todaysMentor', async (request) => {
  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) => {
  // Execute any logic that should take place after the object is saved.
}, (error) => {
});
  return results;
});

Parse.Cloud.job('pickTodaysMentor', async function(request) {
  const { params, headers, log, message } = request;
  Parse.Cloud.run('todaysMentor', (request) => {
      if (!passesValidation(request.object)) {
      throw 'Ooops something went wrong'; 
    }
  });
});

我想从我的Mentor类中获取随机Mentor对象,并将其添加到Today类中。这样,我可以在移动应用程序中获取“今日”对象。当我用Swift调用它时,第一个函数运行良好。

我的服务器记录是这样的;

May 13, 2019, 22:22:45 +03:00- ERROR
(node:41) UnhandledPromiseRejectionWarning: TypeError: Parse.Cloud.run is not a function
    at Parse.Cloud.job (/opt/app-root/src/repo/cloud/functions.js:28:19)
    at Object.agenda.define [as fn] (/opt/app-root/src/build/agenda/agenda.js:74:25)
    at process._tickCallback (internal/process/next_tick.js:68:7)
May 13, 2019, 22:22:45 +03:00- ERROR
(node:41) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). (rejection id: 4)

我搜索了此错误,并发现它是Parse 3.0的语法错误,因此他们更改了一些函数语法。 我该如何解决?您对使用此算法有何建议?

谢谢!

1 个答案:

答案 0 :(得分:0)

我建议您使用类似这样的东西:

async function todaysMentor() {
  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]);
  await today.save();
  return results;
}

Parse.Cloud.define('todaysMentor', async (request) => {
  return await todaysMentor();
});

Parse.Cloud.job('pickTodaysMentor', async function(request) {
  return await todaysMentor();
});