How to initialize a ParseObject in a cloud function?

109 Views Asked by At

I want to generate a list of ParseObjects based on the cached data I get from Redis. Here's what I’m doing:

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

This is what I do on the client to fetch the above processed data:

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

When I'm receiving the response, I'm getting this:

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)

If I print an element of the returned array, I get this:

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

What am I missing?

1

There are 1 best solutions below

0
On BEST ANSWER

Turns out there's a static function of the Parse.Object class called fromJSON. What it does, is exactly what I needed... it constructs a ParseObject based on a JSON's data!

Here's the updated function:

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

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

    return hashtags;
}