1

我正在覆盖BeforeSavein CloudCodefor Parse.com。这是我在logs

Input: {"original":null,"update":{"password":"5428","phone_number":"56858","ACL":{"T1pXElsPPT":{"read":true,"write":true}},"config":"{}","creator":"T1pXElsPPT","name":"quarta prova"}}
Result: Update changed to {"config":"{}","creator":"T1pXElsPPT","name":"quarta prova","password":"5428","phone_number":"56858","ACL":{"T1pXElsPPT":{"read":true,"write":true}}}

我只需要检查BeforeSave用户是否正在创建new对象,而不是当他是updating一个对象时old。这是我到目前为止所做的:

Parse.Cloud.beforeSave("tracker", function(request, response) {
var spaces = availableSpace(request.user.id);
if (request.original != undefined && request.original === null) {
    if (spaces == 0) {
        response.error("No available spaces");
    } else if (spaces >= 1) {
        response.success();
    } else if (spaces < 0) {
        response.error("Lookup error");
    }
} else {
    response.success();
}

});

但它似乎original不在请求对象中(所以它是未定义的,总是导致 a response.success();

有没有办法检查original或另一种方法来检查这是一个insert还是一个update

4

1 回答 1

4

您可以使用Parse.Object.isNew()

Parse.Cloud.beforeSave("tracker", function(request, response) {
  var spaces = availableSpace(request.user.id);
  if (request.object.isNew()) {

    if (spaces === 0) {
        response.error("No available spaces");
    } else if (spaces >= 1) {
        response.success();
    } else if (spaces < 0) {
        response.error("Lookup error");
    }

  } else {
      response.success();
  }
});
于 2014-10-21T10:06:02.837 回答