我正在尝试制作一个具有每日报价逻辑并显示报价的应用程序。它应该在我的random object
中选择parse class
并将其显示给用户。如果用户看到了今天的对象,他们应该不会在同一天看到其他随机对象。
我用Swift
制作了此算法。但是我认为Cloud Code
和Background 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
的语法错误,因此他们更改了一些函数语法。 我该如何解决?或您对使用此算法有何建议?
谢谢!
答案 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();
});