beforeSave在自定义云功能中多次触发

时间:2013-11-20 18:03:59

标签: javascript backbone.js cloud httprequest parse-platform

我创建了一个名为Photo的类,并且有一个大小字段,其中包含图像Parse文件的所有维度(例如:原始缩略图,...)。

我在Photo云函数 beforeSave()中写的图像处理。

我编写了第二个自定义云函数AddUserProfilePhoto,它可以接收上传的Parse文件并使用云代码创建一个新的Photo对象。

我的问题是第二个云函数AddUserProfilePhoto,它使用上传的Parse文件创建Photo对象,并在保存用户时将Photo对象指定给用户作为指针对象和Photo对象的beforeSave()函数被触发多次,直到响应错误。

/* 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);
      }
 });
}); 

始终回复错误:

AddUserProfilePhoto响应错误:错误Domain = Parse Code = 141“操作无法完成。(解析错误141。)”UserInfo = 0x15e1e9e0 { code = 141,error =未捕获尝试在iOS客户端中使用指向新未保存对象的对象保存对象。}。

云控制台多次(近50次)为用户UserObjectId... 的照片触发了 before_save。保存了许多缩略图,但只有一个缩略图图像文件保留在Photo对象中。

0 个答案:

没有答案