Parse.Cloud.beforeSave 无法在正在创建的对象上设置新字段
Parse.Cloud.beforeSave not able to set a new field on an object being created
我在 Parse-Server
上的 Parse.Cloud.beforeSave
函数中遇到了一个小问题。这条线让我感到困惑:
request.object.set("dbCount", 0);
之前一直有效。但是自从我进行了服务器更新后,按照 HEROKU
的要求进入 heroku-18 堆栈(cedar-14 堆栈即将结束生命)。它不再有效。
此行的目的是在数据库中存储新项目时将计数器初始化为零。但是,尽管该项目已按预期创建,但缺少字段 "dbCount"。这是为什么?创建字段的正确代码是什么?
作为参考,这里是beforeSave函数的相关代码:
Parse.Cloud.beforeSave("TheCollection", async (request) => {
if (!request.object.isNew()) return;
var query;
query = new Parse.Query("TheCollection");
query.equalTo("TITLE",request.object.get("TITLE"));
await query.find().then
(function(resUnit) {
// If there is already a Unit with the same Title, we return an error.
if (resUnit.length) throw "TITLE-USED";
var query;
query = new Parse.Query("TheCollection");
var token = request.user.getSessionToken();
query.find({sessionToken: token}).then
(function(resUnit) {
... some useful processing ...
// This next line is not working as it used to.
request.object.set("dbCount", 0);
});
});
});
您可能遗漏了 "return" 并且保存在您的承诺解决之前发生。
我还看到您在嵌套范围内使用相同的变量 "resUnit",这可能会混淆它的使用。
Parse.Cloud.beforeSave("TheCollection", async (request) => {
if (!request.object.isNew()) return;
var query;
query = new Parse.Query("TheCollection");
query.equalTo("TITLE",request.object.get("TITLE"));
await query.find().then
(function(resUnit) {
// If there is already a Unit with the same Title, we return an error.
if (resUnit.length) throw "TITLE-USED";
var query;
query = new Parse.Query("TheCollection");
var token = request.user.getSessionToken();
>>>> return <<<<< query.find({sessionToken: token}).then
(function(resUnit) {
... some useful processing ...
// This next line is not working as it used to.
request.object.set("dbCount", 0);
});
});
});
如果您想更进一步,我会充分利用 async 和 await 来像这样扁平化您的代码。通过使用平面代码,您可以消除很多导致错误的混乱情况
Parse.Cloud.beforeSave("TheCollection", async (request) => {
if (!request.object.isNew()) return;
const query = new Parse.Query("TheCollection");
query.equalTo("TITLE",request.object.get("TITLE"));
const resUnit1 = await query.first()
// If there is already a Unit with the same Title, we return an error.
if (resUnit) throw "TITLE-USED";
const query2 = new Parse.Query("TheCollection");
const resUnit2 = await query2.find({sessionToken: request.user.getSessionToken()})
... some useful processing ...
request.object.set("dbCount", 0);
});
我在 Parse-Server
上的 Parse.Cloud.beforeSave
函数中遇到了一个小问题。这条线让我感到困惑:
request.object.set("dbCount", 0);
之前一直有效。但是自从我进行了服务器更新后,按照 HEROKU
的要求进入 heroku-18 堆栈(cedar-14 堆栈即将结束生命)。它不再有效。
此行的目的是在数据库中存储新项目时将计数器初始化为零。但是,尽管该项目已按预期创建,但缺少字段 "dbCount"。这是为什么?创建字段的正确代码是什么?
作为参考,这里是beforeSave函数的相关代码:
Parse.Cloud.beforeSave("TheCollection", async (request) => {
if (!request.object.isNew()) return;
var query;
query = new Parse.Query("TheCollection");
query.equalTo("TITLE",request.object.get("TITLE"));
await query.find().then
(function(resUnit) {
// If there is already a Unit with the same Title, we return an error.
if (resUnit.length) throw "TITLE-USED";
var query;
query = new Parse.Query("TheCollection");
var token = request.user.getSessionToken();
query.find({sessionToken: token}).then
(function(resUnit) {
... some useful processing ...
// This next line is not working as it used to.
request.object.set("dbCount", 0);
});
});
});
您可能遗漏了 "return" 并且保存在您的承诺解决之前发生。 我还看到您在嵌套范围内使用相同的变量 "resUnit",这可能会混淆它的使用。
Parse.Cloud.beforeSave("TheCollection", async (request) => {
if (!request.object.isNew()) return;
var query;
query = new Parse.Query("TheCollection");
query.equalTo("TITLE",request.object.get("TITLE"));
await query.find().then
(function(resUnit) {
// If there is already a Unit with the same Title, we return an error.
if (resUnit.length) throw "TITLE-USED";
var query;
query = new Parse.Query("TheCollection");
var token = request.user.getSessionToken();
>>>> return <<<<< query.find({sessionToken: token}).then
(function(resUnit) {
... some useful processing ...
// This next line is not working as it used to.
request.object.set("dbCount", 0);
});
});
});
如果您想更进一步,我会充分利用 async 和 await 来像这样扁平化您的代码。通过使用平面代码,您可以消除很多导致错误的混乱情况
Parse.Cloud.beforeSave("TheCollection", async (request) => {
if (!request.object.isNew()) return;
const query = new Parse.Query("TheCollection");
query.equalTo("TITLE",request.object.get("TITLE"));
const resUnit1 = await query.first()
// If there is already a Unit with the same Title, we return an error.
if (resUnit) throw "TITLE-USED";
const query2 = new Parse.Query("TheCollection");
const resUnit2 = await query2.find({sessionToken: request.user.getSessionToken()})
... some useful processing ...
request.object.set("dbCount", 0);
});