保存Parse对象

时间:2016-04-21 14:59:20

标签: javascript node.js amazon-web-services parse-platform aws-lambda

我的aws-lambda函数中有一个异步函数,效果很好。

这样做:

  1. 抓取图片('下载'功能),

  2. 裁剪并将其调整为缩略图('转换'功能),

  3. 将该缩略图上传到新存储区(“上传”功能),

  4. 使用该缩略图的网址('updateVenue'函数)

  5. 更新场地对象(这是一个Parse对象)
  6. 最后,它创建了一个新的场景对象(这也是一个解析对象)('saveScene'函数)。

  7. **我省略了代码,指明venueObj和sceneObj是什么让它更简单,因为我不相信这是问题。

    我的问题是,在updateVenue函数被记录为成功完成后,下一个日志是:在完成请求之前退出进程。 Aka saveScene函数永远不会被调用。

    即使我按翻转updateVenue和saveScene函数的顺序,也会在第一个Parse函数 - saveScene完成后退出该进程。因此,我认为错误是我称之为的方式。

    我也在使用 context.succeed(),也许这与它有关?

    // Download the image from S3, transform, and upload to a different S3 bucket.
            async.waterfall([
                function download(next) {
                    // Download the image from S3 into a buffer.
                    s3.getObject({
                            Bucket: srcBucket,
                            Key: srcKey
                        },
                        next);
                    },
                function transform(response, next) {
                    gm(response.Body).size(function(err, size) {
                        // Infer the scaling factor to avoid stretching the image unnaturally.
                        WIDTH = size.width;
                        HEIGHT = size.height;
    
    
                        if (WIDTH > HEIGHT) {
                            var side = HEIGHT;
                        }
                        else{
                            var side = WIDTH;
                        }
    
                        var scalingFactor = Math.min(
                                        MAX_WIDTH / side,
                                        MAX_HEIGHT / side
                                    );
                                    var width  = scalingFactor * side;
                                    var height = scalingFactor * side;
    
                        // Transform the image buffer in memory.
                        this.gravity("Center").crop(side, side).resize(width, height)
                            .toBuffer(imageType, function(err, buffer) {
                                if (err) {
                                    next(err); 
                                    console.log(err);
                                } else {
                                    next(null, response.ContentType, buffer);
    
                                }
                            });
    
    
                    });
                },
                function upload(contentType, data, next) {
                    // Stream the transformed image to a different S3 bucket.
                    s3.putObject({
                            Bucket: dstBucket,
                            Key: dstKey,
                            Body: data,
                            ContentType: contentType
                        },
                        next);
                },
                function updateVenue(next) {
                    venueObj.save(null, {
                      success: function(response){
                        console.log('Updated Venue thumbnail succesfully: ', response);
                        context.succeed();
                        next
                      },
                      error: function(response, error){
                          console.log('Failed to update Venue thumbnail, with error code: ' + error.description);
                          context.fail();
                          next
                      }
                    }); // end of venueObj.save
                },
                function saveScene(next) {
                    sceneObj.save(null, {
                      success: function(response){
                        console.log('Saved sceneObj succesfully: ', response);
                        context.succeed();
                        next
                      },
                      error: function(response, error){
                          console.log('Failed to create new sceneObj, with error code: ' + error.description);
                          context.fail();
                          next 
                      }
                    }); // end of sceneObj.save
                }
                ], function (err) {
                    if (err) {
                        console.error(
                            'Unable to resize ' + srcBucket + '/' + srcKey +
                            ' and upload to ' + dstBucket + '/' + dstKey +
                            ' due to an error: ' + err
                        );
                    } else {
                        console.log(
                            'Successfully resized ' + srcBucket + '/' + srcKey +
                            ' and uploaded to ' + dstBucket + '/' + dstKey
                        );
                    }
    
                    callback(null, "message");
                }
    
    
            );
    

2 个答案:

答案 0 :(得分:0)

我相信你只需要在updateVenue和saveScence中调用if(isChecked){ //do your stuff with the button } 。 async.waterfall将回调传递给系列中的每个函数,您当前正在使用next。如果需要将数据传递给下一个fn,则将其作为第二个参数传递给回调。

以下是next

中如何应用此示例的示例
updateVenue

希望有所帮助!

答案 1 :(得分:0)

我发现了如何解决这个问题。不幸的是,我无法将功能分开,但是,我能够将第二个功能嵌入到第一个功能的完成块中:

// Download the image from S3, transform, and upload to a different S3 bucket.
        async.waterfall([
            function download(next) {
                // Download the image from S3 into a buffer.
                s3.getObject({
                        Bucket: srcBucket,
                        Key: srcKey
                    },
                    next);
                },
            function transform(response, next) {
                gm(response.Body).size(function(err, size) {
                    // Infer the scaling factor to avoid stretching the image unnaturally.
                    WIDTH = size.width;
                    HEIGHT = size.height;


                    if (WIDTH > HEIGHT) {
                        var side = HEIGHT;
                    }
                    else{
                        var side = WIDTH;
                    }

                    var scalingFactor = Math.min(
                                    MAX_WIDTH / side,
                                    MAX_HEIGHT / side
                                );
                                var width  = scalingFactor * side;
                                var height = scalingFactor * side;

                    // Transform the image buffer in memory.
                    this.gravity("Center").crop(side, side).resize(width, height)
                        .toBuffer(imageType, function(err, buffer) {
                            if (err) {
                                next(err); 
                                console.log(err);
                            } else {
                                next(null, response.ContentType, buffer);

                            }
                        });


                });
            },
            function upload(contentType, data, next) {
                // Stream the transformed image to a different S3 bucket.
                s3.putObject({
                        Bucket: dstBucket,
                        Key: dstKey,
                        Body: data,
                        ContentType: contentType
                    },
                    next);
            },
            function updateVenue(next) {
                venueObj.save(null, {
                  success: function(response){
                    console.log('Updated Venue thumbnail succesfully: ', response);
                    sceneObj.save(null, {
                      success: function(response){
                        console.log('Saved sceneObj succesfully: ', response);
                        context.succeed();
                        next
                      },
                      error: function(response, error){
                          console.log('Failed to create new sceneObj, with error code: ' + error.description);
                          context.fail();
                          next 
                      }
                    }); // end of sceneObj.save

                  },
                  error: function(response, error){
                      console.log('Failed to update Venue thumbnail, with error code: ' + error.description);
                      context.fail();
                      next
                  }
                }); // end of venueObj.save
            }
            ], function (err) {
                if (err) {
                    console.error(
                        'Unable to resize ' + srcBucket + '/' + srcKey +
                        ' and upload to ' + dstBucket + '/' + dstKey +
                        ' due to an error: ' + err
                    );
                } else {
                    console.log(
                        'Successfully resized ' + srcBucket + '/' + srcKey +
                        ' and uploaded to ' + dstBucket + '/' + dstKey
                    );
                }

                callback(null, "message");
            }


        );