gpt4 book ai didi

javascript - 解析预定的后台作业

转载 作者:行者123 更新时间:2023-11-30 10:44:12 26 4
gpt4 key购买 nike

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

我用 Swift 编写了这个算法。但我认为Cloud CodeBackground Job是执行此算法的更清晰和正确的方法。我研究了后台作业教程指南等来做到这一点,但我做不到,因为我没有足够的 JavaScript 知识来做到这一点。无论我在我的解析服务器中创建什么后台作业,就像这样;

    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)

我用 google 搜索了这个错误,并了解到这是 Parse 3.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();
});

关于javascript - 解析预定的后台作业,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56125188/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com