Parse.com识别更新或在BeforeSave中插入

时间:2014-10-21 07:36:53

标签: javascript parse-platform cloud-code before-save

我正在覆盖BeforeSave CloudCode中的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

});

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

有没有办法检查insert或其他方式来检查这是update还是{{1}}?

1 个答案:

答案 0 :(得分: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();
  }
});