【问题标题】:Parse Scheduled Background Jobs解析预定的后台作业
【发布时间】:2019-05-14 07:36:42
【问题描述】:

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

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

    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 类中。通过这种方式,我可以在我的移动应用程序中获取 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 的语法错误,他们更改了一些函数语法。 我该如何解决这个问题?您对制作此算法有什么建议吗?

谢谢!

【问题讨论】:

  • 您运行的 Parse Server 版本是什么?
  • 嘿兄弟,它在 Sashido.com 上,服务器版本是 1.17.2
  • 您能否尝试将您的 Parse Server 更新到最新版本? 1.17.2 版本可能不支持 Parse.Cloud.run 功能。
  • 此应用程序的当前 Parse Server 版本是 v3.1.3

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


【解决方案1】:

我建议你这样做:

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();
});

【讨论】:

    猜你喜欢
    • 2016-02-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-04-10
    • 2022-06-13
    • 2022-11-06
    • 1970-01-01
    相关资源
    最近更新 更多