简体   繁体   English

解析云函数返回“ undefined”

[英]Parse Cloud Function returning “undefined”

I'm trying to get the Parse Cloud Module example, and turns into a separated method (not one callback as current is on Parse Docs Example). 我正在尝试获取“解析云模块”示例,并将其转换为一个单独的方法(“解析文档示例”中没有最新的回调)。

Every time I try to call that function, I get the follow message, and then nothing happens: 每次尝试调用该函数时,都会收到以下消息,然后什么也没有发生:

I2015-04-20T23:46:47.073Z] v19: Ran cloud function generateProfilePhotoThumbnail for user uX9PTGGfji with: Input: {"objectId":"uX9PTGGfji"} Result: undefined I2015-04-20T23:46:47.073Z] v19:用户uX9PTGGfji的跑云函数generateProfilePhotoThumbnail具有:输入:{“ objectId”:“ uX9PTGGfji”}结果:未定义

I'm not a master on Javascript. 我不是Java的高手。 I guess I made some mistake. 我想我弄错了。 Please, someone can review my code and teach me what's wrong ? 拜托,有人可以查看我的代码并教我什么地方错了? Thank's !! 谢谢 !!

var Image = require("parse-image");

Parse.Cloud.define("generateProfilePhotoThumbnail", function(request, response) {
  var query = new Parse.Query(Parse.User);
  query.equalTo("objectId", request.params.objectId);
  query.first({
    success: function(foundUser) {
      var user = foundUser;
      Parse.Cloud.httpRequest({
        url: user.get("profilePhoto").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 64x64.
        return image.scale({
          width: 64,
          height: 64
        });

      }).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.
        user.set("profilePhotoThumbnail", cropped);

      }).then(function(result) {
        response.success(result);
      }, function(error) {
        response.error(error);
      });
    },
    error: function(error) {
      response.error("generateProfilePhotoThumbnail failed" + error);
    }
  });
});

From the looks of it query.first() is an asynchronous call and the function returns after making this call, thus the result is undefined. 从它的外观看,query.first()是一个异步调用,该函数在执行此调用后返回,因此结果是不确定的。 Try 尝试

query.first().then(function(foundUser){
   //put rest of your code here
}, function(error) {
   response.error(error);
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM