0

Im not a javascript dev but i have to use it for my backend. For some reason coming from an objective c background i cannot perform the most simple tasks in javascript. All i want to do is save this string that is in the variable "thePlayer" to my backend under the key "playerName." Ignore everything else. its all about lines 2, 13, and 24. My cloud log keeps saying the key "playerName" was expecting a string but got an object. So my question is why is thePlayer getting saved as an object?

Parse.Cloud.define("getUsers", function(request, response) {
    var thePlayer = new String(); <<<***********************************************
    var query = new Parse.Query(Parse.User);
query.find({
    success: function(results){
        var users = [];
        //extract out user names from results
        for(var i = 0; i < results.length; ++i){
            users.push(results[i].id);
        }
        var rand = users[Math.floor(Math.random() * users.length)];
        console.log("The random user is" + rand);
        thePlayer = rand;   <<<******************************************************
        response.success(users);
    }, error: function(error){
        response.error("Error");
    }
});

var GameScore = Parse.Object.extend("Messages");
var gameScore = new GameScore();


gameScore.set("score", 1337);
gameScore.set("playerName", thePlayer);  <<<**************************************************
gameScore.set("cheatMode", false);

gameScore.save(null, {
  success: function(gameScore) {
// Execute any logic that should take place after the object is saved.
alert('New object created with objectId: ' + gameScore.id);
  },
  error: function(gameScore, error) {
// Execute any logic that should take place if the save fails.
// error is a Parse.Error with an error code and message.
alert('Failed to create new object, with error code: ' + error.message);
  }
});
});
4
  • Use one = for assignment, two == for comparison. Commented Sep 15, 2014 at 19:14
  • That was one of my edits that i made before my post to try to fix it. I changed it back and it still says its an object. Commented Sep 15, 2014 at 19:18
  • "Im not a javascript dev but i have to use it for my backend" - does that mean you are using Node.js? Commented Sep 15, 2014 at 19:36
  • No im using javascript for my cloud code on parse.com Commented Sep 16, 2014 at 1:36

2 Answers 2

1

I think it is because when you defined thePlayer, it is a string object (see example below)

http://jsfiddle.net/uhpwgoum/

and when you try to set it to playerName, the query has not yet finished (success function not called yet) so you are setting the string object to playerName therefore you get that error

If you defined thePlayer like see example here http://jsfiddle.net/uhpwgoum/1/

var thePlayer = "";

you won't get that error, but your playerName will probably be empty string if the query is not finished.

Sign up to request clarification or add additional context in comments.

2 Comments

yep that definitely helped. thePlayer is getting passed as a string now but like you said it is empty. Any suggestions about how to fix this? is there a wait call?
Set playerName in the success function, so you know for sure it will be that rand variable. I don't know how your program runs but it might be better to initialize gameScore in the success. Try it and see how it works, if you run into trouble, you can always post another question
1

Try : gameScore.set("playerName", thePlayer.toString());

that should work.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.