我正在尝试使用beforeSave触发器自动为数据对象设置ACL。然而,有时候即使正确保存对象,看起来随机也不会触发beforeSave方法。
我有一个最小的例子,对于一个或两个对象几乎总是失败。这是云代码:
Parse.Cloud.beforeSave("TestObj", function(request, response) {
if (!request.object.existed()) {
request.object.setACL(new Parse.ACL(request.user));
}
response.success();
});
这里创建了12个用户,每个用户都有一个数据对象(我只是在firefox中本地运行):
function initUsers() {
Parse.initialize("TheApplicationId", "TheClientKey");
Parse.User.logOut();
const TestObj = Parse.Object.extend("TestObj", {}, {});
minimalInitTest([new TestObj(), new TestObj(), new TestObj(), new TestObj(),
new TestObj(), new TestObj(), new TestObj(), new TestObj(),
new TestObj(), new TestObj(), new TestObj(), new TestObj()], 0);
}
function minimalInitTest(objs, idx) {
if (idx < objs.length) {
var u = new User();
const email = "test" + idx + "@example.com";
u.set("username", email);
u.set("password", "test");
u.set("email", email);
u.signUp(null, {
success: function(user) {
user.set("data", objs[idx]);
user.save(null, {
success: function(o) {
minimalInitTest(objs, idx+1);
},
error: function(e) { console.error(e); }
});
},
error: function(e) { console.error(e); }
});
}
}
以下是来自通话的示例日志。可以看出,即使创建了12个对象,beforeSave触发器也只被调用了9次。
I2015-05-28T12:40:26.498Z]v100 before_save triggered for TestObj for user hMX2TQdKlZ:
Input: {"original":null,"update":{}}
Result: Update changed to {"ACL":{"hMX2TQdKlZ":{"read":true,"write":true}}}
I2015-05-28T12:40:27.826Z]v100 before_save triggered for TestObj for user k8OTAas7mg:
Input: {"original":null,"update":{}}
Result: Update changed to {"ACL":{"k8OTAas7mg":{"read":true,"write":true}}}
I2015-05-28T12:40:28.475Z]v100 before_save triggered for TestObj for user ztrsg7k7Mj:
Input: {"original":null,"update":{}}
Result: Update changed to {"ACL":{"ztrsg7k7Mj":{"read":true,"write":true}}}
I2015-05-28T12:40:29.314Z]v100 before_save triggered for TestObj for user CzyjZdmHwm:
Input: {"original":null,"update":{}}
Result: Update changed to {"ACL":{"CzyjZdmHwm":{"read":true,"write":true}}}
I2015-05-28T12:40:29.945Z]v100 before_save triggered for TestObj for user Tg6h5VjZ5t:
Input: {"original":null,"update":{}}
Result: Update changed to {"ACL":{"Tg6h5VjZ5t":{"read":true,"write":true}}}
I2015-05-28T12:40:30.590Z]v100 before_save triggered for TestObj for user S73xIzKBgL:
Input: {"original":null,"update":{}}
Result: Update changed to {"ACL":{"S73xIzKBgL":{"read":true,"write":true}}}
I2015-05-28T12:40:31.365Z]v100 before_save triggered for TestObj for user 0fsea0JDbF:
Input: {"original":null,"update":{}}
Result: Update changed to {"ACL":{"0fsea0JDbF":{"read":true,"write":true}}}
I2015-05-28T12:40:32.021Z]v100 before_save triggered for TestObj for user McHukXGAGi:
Input: {"original":null,"update":{}}
Result: Update changed to {"ACL":{"McHukXGAGi":{"read":true,"write":true}}}
I2015-05-28T12:40:32.609Z]v100 before_save triggered for TestObj for user ebBQcCV4zi:
Input: {"original":null,"update":{}}
Result: Update changed to {"ACL":{"ebBQcCV4zi":{"read":true,"write":true}}}