0

当我使用下面的代码访问当前用户的类行时?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"}

解析云代码:

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

1 回答 1

0

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

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

然后可以通过键循环:

for (var key in keys) { ...

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

于 2014-09-17T16:45:55.337 回答