ParseError:'用户名错误或缺失'

时间:2017-06-21 21:59:33

标签: javascript swift parse-platform cloud-code

所以我有一些我想写的云代码,就像帖子一样。

我的数据库设置为用户拥有likedPosts数组,其中包含用户喜欢的所有帖子的对象ID。用户还有一个列coins,当用户喜欢他们的帖子时,该列应该会增加。

post对象有一个likes列,它是一个整数,每个都会增加,而post对象也有一个posterId列,这是发布它的用户的对象ID

这是我现在的功能(我不擅长javascript和云代码,所以如果有可怕的错误,我很抱歉)

Parse.Cloud.define("likePost", function(request, response) {

    Parse.Cloud.useMasterKey();

    var senderId = request.params.senderId;
    var postId = request.params.postId;

    var post = new Parse.Object ({objectId: postId});
    var posterId = post.posterId

    var poster = new Parse.User ({objectId: posterId});

    var sender = new Parse.User ({objectId: senderId});

    sender.add("likedPosts", postId);

    poster.increment("coins");

    post.increment("likes");

    poster.save(null, {useMasterKey:true, success: 
        function(poster) {
            console.log("Successfully saved poster");   
    }, error: function(poster, error) {
            console.error("Error saving poster: " + error.message);
            response.error(error);
        }
    });
    post.save(null,{useMasterKey:true, success: 
            function(post) {
                console.log("Successfully saved post"); 
        }, error: function(post, error) {
                console.error("Error saving post: " + error.message);
                response.error(error);
            }
        });
    sender.save(null, {useMasterKey:true, success: 
            function(sender) {
                console.log("Successfully saved sender");   
        }, error: function(sender, error) {
                console.error("Error saving sender: " + error.message);
                response.error(error);
            }
        });

    response.success();
});

我像swift一样调用函数:

PFCloud.callFunction(inBackground: "likePost", withParameters: ["senderId" : PFUser.current()!.objectId!, " postId": postObject!.objectId!], block: { (result, error) in

        if (error != nil) {
            print(error!)
        } else {
            print("success liking")
        }

    })

但是,在我的日志中,出现以下错误:

2017-06-21T21:47:59.499Z - Failed running cloud function likePost for user R4d8Zrcdhw with:
  Input: {"senderId":"R4d8Zrcdhw"," postId":"XXbu55PdpR"}
  Error: {"code":141,"message":{"code":200,"message":"bad or missing username"}}
2017-06-21T21:47:59.492Z - bad or missing username

1 个答案:

答案 0 :(得分:0)

我的猜测是该请求缺少定义内容类型的标头。我已经看到,如果Swift URLSession使用了不正确的内容类型标头,则Parse会通过Parse REST API返回“错误或缺少用户名”错误。

request.addValue("application/json", forHTTPHeaderField: "Content-Type")

Parse.Cloud.httpRequest({
  url: 'http://www.example.com/',
  headers: {
    'Content-Type': 'application/json;charset=utf-8'
  }
})