javascript - 解析云代码作业功能优化
问题描述
我有一份正在运行的云代码工作。当我手动运行作业时,它似乎工作。我认为调度对其设置和运行频率造成了问题,因此我认为它与实际代码无关。也许我错了,但很好奇是否有更有效的方法来轮询我的解析类。我有一个应用程序,我试图在其中查找从 now() 开始的下一小时内即将到来的预订,并且如果有向该类中的用户发送推送通知。同样,这会运行,但我认为我可能能够优化我的查询以仅在该时间范围内获取项目而不是具有特定状态的更多项目。
Parse.Cloud.job("updateReviews", async (request) => {
var resultCount = 0;
// Query all bookings with a status of confirmed
var query = new Parse.Query("bookings");
query.equalTo("status", "confirmed");
const results = await query.find({useMasterKey:true});
results.forEach(object => {
var users = [];
users.push(object.get("buyerId"));
users.push(object.get("placeOwner"));
var today = new moment();
var hourFrom = moment().add(1, 'hours');
var startTime = moment(object.get("startTime"));
if (startTime.isBetween(today, hourFrom)) {
console.log("BETWEEN THE TIMEFRAME");
resultCount += 1;
users.forEach(sendPush);
} else {
console.log("NOT BETWEEN THE TIME FRAME, PASS OVER");
}
});
return ("Successfully sent " + resultCount + " new notifications!");
});
函数发送推(值,索引,数组){
var pushType = "bookingConfirmed";
let query = new Parse.Query(Parse.Installation);
query.equalTo("userId", value);
return Parse.Push.send({
where: query,
data: {
title: "New Booking Coming Up",
alert: "You have a booking coming up soon!",
pushType
}
},{useMasterKey: true});
}
解决方案
是的。它可能会好得多。我会尝试这样的事情:
Parse.Cloud.job('updateReviews', async () => {
// Query all bookings with a status of confirmed
const query = new Parse.Query('bookings');
query.equalTo('status', 'confirmed');
const now = new Date();
query.greaterThanOrEqualTo('startTime', now);
query.lessThanOrEqualTo('startTime', new Date(now.getTime() + 60 * 60 * 1000));
const results = await query.find({ useMasterKey: true });
const pushType = "bookingConfirmed";
const pushQuery = new Parse.Query(Parse.Installation);
pushQuery.containedIn("userId", results.map(result => result.get('buyerId')).concat(results.map(result => result.get('placeOwner'))));
await Parse.Push.send({
where: pushQuery,
data: {
title: 'New Booking Coming Up',
alert: 'You have a booking coming up soon!',
pushType
}
}, { useMasterKey: true });
return (`Successfully sent ${results.length} new notifications!`);
});
推荐阅读
- postgresql - json aws dms postgres 类型的输入语法无效
- ruby-on-rails - 如何获取属于某个父模型的所有子模型(Ruby on Rails)
- javascript - Javascript - 观察表单元素的第一次交互/更改
- android - 如何在 Android v14 设备上使用 Firebase Crashlytics
- python - 从 pythonanywhere venv 的 apschedular 导入尝试导入错误的 calendar.py
- c++ - C++ 代码。谁能帮我理解为什么这不起作用?
- css - flexbox 中有什么东西可以让容器的孩子在容器溢出时转到另一行吗?
- javascript - 当连接并从 BLE 设备接收数据时,出现 DOM 异常:不允许 GATT 服务器操作
- python - python爬取网站时如何处理动态cookies?
- angular - 如何更改此异步管道并直接取值?