如何在云函数中初始化 ParseObject?

How to initialize a ParseObject in a cloud function?

我想根据从 Redis 获得的缓存数据生成 ParseObject 的列表。这是我正在做的事情:

function parseHashtagsResponse(cachedObjects) {
    var hashtags = [];

    for (const object of cachedObjects) {
        const Hashtag = Parse.Object.extend("Hashtag");
        const hashtag = new Hashtag();

        for (var key in object) {
            if (object.hasOwnProperty(key)) {
                hashtag.set(key, object[key]);
            }
        }

        hashtags.push(hashtag);
    }

    return hashtags;
}

cachedObjects is an array of dictionaries that contains the data of cached ParseObjects

这是我在客户端获取上述处理后的数据所做的:

PFCloud.callFunction(inBackground: "getHashtagsFunctionV3", withParameters: nil) { (response, error) in
    if error == nil, let hashtagsArray = response as? [PFObject] {
        self.hashtagsArray.append(contentsOf: hashtagsArray)
    }
}

当我收到回复时,我得到了这个:

expression produced error: error: /var/folders/1h/vqb7yf6n14d52q7zdmgbx3lr0000gn/T/expr30-d84d97..swift:1:83: error: 'PFObject' is not a member type of 'Parse' Swift._DebuggerSupport.stringForPrintObject(Swift.UnsafePointer<Swift.Array<Parse.PFObject>>(bitPattern: 0x11e0e08f0)!.pointee)

如果我打印返回数组的一个元素,我会得到:

<Hashtag: 0x60000331d320, objectId: hLuDPG45CI, localId: (null)> {}

我错过了什么?

原来 Parse.Object class 有一个名为 fromJSON 的静态函数。它所做的,正是我所需要的...它根据 JSON 的数据构造了一个 ParseObject

这是更新后的函数:

function parseResponse(cachedObjects, className) {
    var hashtags = [];

    for (var object of cachedObjects) {
        object.className = className;
        hashtags.push(Parse.Object.fromJSON(object));
    }

    return hashtags;
}