0

When I use the code below to access a class row for the current user? Parse reports that the function returns a data set thus:

{"user":{"__type":"Pointer""className":"_User""objectId":"NFYHCP6Ftw"}
"known_fieldname":"Value"
"known_fn2":"value2"
"known_fn3":"value3"
"objectId":"obFbHtMW4E"
"createdAt":"2014-09-16T15:47:55.047Z"
"updatedAt":"2014-09-16T16:10:55.318Z"
"__type":"Object"
"className":"Answers"}

Parse CloudCode:

Parse.Cloud.define("Answers", function(request, response) {
    var query = new Parse.Query("Answers");
    query.equalTo("user", Parse.User.current());
    query.first({
        success: function(obj) {
            // kick out "value"...
            console.log("A field I know="+obj.get("known_fieldname"));
            // this for loop doesn't loop thru the field "keys" and "values"
            // what do I replace it with?
            for (var key in obj) {
                if (obj.hasOwnProperty(key)) {
                    console.log("Answer="+obj.get(key));
                    // add to a dictionary to return to caller
                }
            }
            response.success(obj);
        },
        error: function() {
            response.error("Failed to get any answers.");
        }
    });
});
rici
  • 234,347
  • 28
  • 237
  • 341
Carl
  • 2,896
  • 2
  • 32
  • 50

1 Answers1

0

To get a list of keys for an Object one needs to do this:

var keys = Object.keys(obj.toJSON());

One can then loop thru keys thus:

for (var key in keys) { ...

I think the Parse developers assumed one would know the names of fields within a Parse class and so didn't provide (yet) a clean API to access them.

Carl
  • 2,896
  • 2
  • 32
  • 50