Monday, 15 July 2013

javascript - Parse.com discern update or insert in BeforeSave -



javascript - Parse.com discern update or insert in BeforeSave -

i'm overriding beforesave in cloudcode parse.com. see in 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 {"config":"{}","creator":"t1pxelsppt","name":"quarta prova","password":"5428","phone_number":"56858","acl":{"t1pxelsppt":{"read":true,"write":true}}}

i need check beforesave if user creating new object, not when he's updating old one. i've done far:

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(); }

});

but seems original not in request object (so it's undefined, leading response.success();)

is there way check original or way check if insert or update?

you can utilize 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(); } });

javascript parse.com cloud-code before-save

No comments:

Post a Comment