当我不知道字段名时,如何访问Parse.com对象的字段?

时间:2014-09-17 15:47:21

标签: parse-platform cloud-code

当我使用下面的代码访问当前用户的类行时? Parse报告该函数返回一个数据集:

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

解析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.");
        }
    });
});

1 个答案:

答案 0 :(得分:0)

要获取Object的键列表,需要执行以下操作:

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

然后可以通过键循环:

for (var key in keys) { ...

我认为Parse开发人员假设人们会知道Parse类中字段的名称,因此没有提供(还)一个干净的API来访问它们。