0

So I have some cloud code I am trying to write to like a post.

My database is setup that users have a likedPosts array, which has object id's of all the posts that the user liked. Users also have a column coins, that should get incremented when users like their posts.

The post object has a likes column which is an integer that gets incremented with each like, and the post object also has a posterId column, which is the object id of the user that posted it.

Here is my function right now (I am not very good at javascript and cloud code, so if there is something horribly wrong, I'm sorry)

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();
});

I call the function from swift like so:

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")
        }

    })

In my logs, however, I get the following error:

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
Minebomber
  • 1,209
  • 2
  • 12
  • 35
  • Have you tested this code as just a basic login to the database? It's not clear what database you're using but most require you to log in first. – Raydot Jun 21 '17 at 22:04
  • @DaveKaye what do you mean? I am logged into the app, and when I run the code I get that error (I use back4app if that's what you're asking) – Minebomber Jun 21 '17 at 22:05
  • Does the app require connection credentials for the database? – Raydot Jun 21 '17 at 22:08
  • @DaveKaye I don't think so? I'm not really sure. I login to the app, go to a post, then call this function on a button tap. – Minebomber Jun 21 '17 at 22:09
  • do a search, there are similar questions https://stackoverflow.com/questions/25217150/200-error-missing-username-when-logging-in-using-parse-and-livecode – N3SS4H Jun 22 '17 at 00:56

1 Answers1

0

My guess is that the request is missing a header to define the content-type. I've seen Parse return the "bad or missing username" error via the Parse REST API if the Swift URLSession was using an incorrect content-type header.

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

or

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