Note that there are some explanatory texts on larger screens.

plurals
  1. PObeforeSave triggered many times in self-defined cloud function
    primarykey
    data
    text
    <p>I create a class is called <code>Photo</code>, and there is a <strong>sizes</strong> filed that includes all dimensions of image Parse file (ex: <em>original</em>, <em>thumbnail</em>, ...).</p> <p>The image manipulations I wrote in the <code>Photo</code> cloud function <em>beforeSave()</em>.</p> <p>And I wrote the second self-defined cloud function <code>AddUserProfilePhoto</code> which can receive the uploaded Parse file and create a new <code>Photo</code> object with cloud code.</p> <p>My problem is the second cloud function <code>AddUserProfilePhoto</code> that it create the <code>Photo</code> object with the uploaded Parse file and assign the <code>Photo</code> object to the user as a pointer, when save the user object and the beforeSave() function of <code>Photo</code> object is triggered many times until respond error.</p> <pre><code>/* Profile Image Thumbnail */ // Parse original file is uploaded, and is being to manipulate to other dimension files and associate to the `Photo` object. Parse.Cloud.beforeSave("Photo", function(request, response) { var userPhoto = request.object; if (!userPhoto.get("sizes")["original"]) { response.error("UserPhoto must have original photo file pointer reference."); return; } Parse.Cloud.httpRequest({ url: userPhoto.get("sizes")["original"].url() }).then(function(response) { var image = new Image(); return image.setData(response.buffer); }).then(function(image) { // Crop the image to the smaller of width or height. var size = Math.min(image.width(), image.height()); return image.crop({ left: (image.width() - size) / 2, top: (image.height() - size) / 2, width: size, height: size }); }).then(function(image) { // Resize the image to 98x98 return image.scale({ width: 98, height: 98 }); }).then(function(image) { // Make sure it's a JPEG to save disk space and bandwidth. return image.setFormat("JPEG"); }).then(function(image) { // Get the image data in a Buffer return image.data(); }).then(function(buffer) { // Save the image into a new file. var base64 = buffer.toString("base64"); var cropped = new Parse.File("thumbnail.jpg", { base64: base64 }); return cropped.save(); }).then(function(cropped) { // Attach the image file to the original object. userPhoto.get("sizes")["thumbnail"] = cropped; // Attach the user pointer to the photo object. userPhoto.set("userObject", request.user); }).then(function(result) { response.success(); }, function(error) { response.error(error); }); }); /* Add user profile photo */ Parse.Cloud.define("AddUserProfilePhoto", function(request, response) { var file = request.params.file; var Photo = Parse.Object.extend("Photo"); var user = request.user; var photo = new Photo(); var sizes = {"original": file}; photo.save({ sizes: sizes }, { success: function(photo) { // check the output, photo object id is existed, so the photo is truly saved in the database. console.log("AddUserProfilePhoto saved photo id: " + photo.id); user.save({ userPhotoObject: photo }, { success: function(user) { console.log("user photo saved, and userId: " + user.id); response.success(user); }, error: function(error) { console.error(error); response.error(error); } }); }, error: function(error) { console.error(error); response.error(error); } }); }); </code></pre> <hr> <p>Always respond the error:</p> <p>AddUserProfilePhoto response error: Error Domain=Parse Code=141 "<strong>The operation couldn’t be completed. (Parse error 141.)</strong>" UserInfo=0x15e1e9e0 {<strong>code=141, error=Uncaught Tried to save an object with a pointer to a new, unsaved object.</strong>} in the iOS client.</p> <p>And the cloud console logged <strong>before_save triggered for Photo for user <code>UserObjectId...</code></strong> many times (nearly 50 times). Saved many thumbnails but only one thumbnail image file leave in the Photo object.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload