mjw
mjw

Reputation: 914

Fetching Object's values from a returned Array of a Query (Parse.com + Node.js)

I've used the Parse SDK before, but for native mobile dev - both Objective-C & Java. However, I am by no means, a JavaScript developer. I'm experimenting with Node and am using Parse to store some stuff for an API im making. This is probably as much a JavaScript incompetency as it is a Parse issue.

App Context

I do a specific twitter scrape each day - a TweetDay - which is basically just a Parse row, with an Array value, this is an Array of TweetPairs.

A TweetPair is just a pairing of the tweet text and its associated id.

{ "id": "NCnjSDnjScn",
"text" : "Be cool Yolanda, be cool!" }

//Look for a Day in the database against the day we supply
query.equalTo("createdAt", req.params.date );
query.find({
    success: function(results) {
        console.log("Successfully retrieved " + results.length + " item");
        // assume there is only ever one result
        var object = results[0];
        console.log(object.id);
        var tweetsPointer = object.get("Pairs");
        //## IT'LL BLOW UP HERE ##
        // tweetsPointer isn't a Parse object, so it doesn't know about .fetch
        tweetsPointer.fetch({
            success: function(tweets) {
                // The object was refreshed successfully.
                console.log(tweets);
                var arr = new Array();
                for (var i = 0; i < tweets.length; i++) { 
                    arr[i] = tweets[i]["text"];
                }
                // just return an array of the tweet messages for that day.
                res.send(arr);
            },
            error: function(myObject, error) {
                // The object was not refreshed successfully.
                // error is a Parse.Error with an error code and description.
            }
        });
    },
    error: function(error) {
        console.log("Error: " + error.code + " " + error.message);
        res.send("Error: " + error.code + " " + error.message);
    }
});

This is what I'm trying to do. Usually in the iOS SDK i could do a query, then call a fetch on the pointers that the query returned to get the actual values (because the query returned PFObject pointers). It's worth noting that i get the correct Parse object id back, and my Pairs array is just a collection of the correct pointers.

What I'm Asking Once a Parse query returns me a result (lazily loaded, no values), how do i then fetch the values for that result. ie: My query returns an Object, with an array of Parse IDs, as opposed the actual values. How do i now populate my Object's array with the values.

What i currently receive

If in the above code, for the success case, i simply return the results object. ie:

success: function(results) {

    // assuming only ever one result
    var object = results[0];
    res.send(object);
...
}

I happily see in my browser

{
    Pairs: [
    {
        __type: "Pointer",
        className: "TweetPair",
        objectId: "wzDNeKJO2n"
    },
    {
        __type: "Pointer",
        className: "TweetPair",
        objectId: "cwXMSPTYEb"
    },
    {
        __type: "Pointer",
        className: "TweetPair",
        objectId: "0FEPlokeIo"
    },
   ..
   ..
    ],

objectId: "5TX1Do98jY",
createdAt: "2014-04-27T07:30:51.658Z",
updatedAt: "2014-04-27T07:30:51.658Z"
}

This is what i expect from my experience with Parse. What i want however is not just the pointers to the tweets, but the tweets themselves.

ie:

{
    Pairs: [
    {
        id: "<twitter id>",
        text: "Be cool yolanda, be cool!"
    },
    {
        id: "sdjvbesjvhBJH",
        text: "Zeds dead baby, Zeds dead."
    },
   ..
   ..
    ],

objectId: "5TX1Do98jY",
createdAt: "2014-04-27T07:30:51.658Z",
updatedAt: "2014-04-27T07:30:51.658Z"
}

This is what the .fetch is usually used for. ie: i would call .fetch on this array, to replace the pointers with their actual values (the tweet messages).

I have been able to do this in the past with the iOS SDK, I'm really just asking about the JS SDK specifically, how do i go about calling fetch on a result. (in iOS the result would already be of type PFObject, so it was easy.)

The Parse Console (for completeness)

The TweetDay object with its array of tweetPairs All of the tweet pairs

Cheers.

Upvotes: 2

Views: 5886

Answers (1)

mjw
mjw

Reputation: 914

So i found this handy little method called .include();

Code as above

query.equalTo("createdAt", req.params.date );
    query.include("Pairs"); // <-----------------------------win
    query.find({
        success: function(results) {
        console.log("Successfully retrieved " + results.length + " item");
                //Assume one result
                var object = results[0];
                var pairs = object.get("Pairs");
                var arr = new Array();
                for (var i = 0; i < pairs.length; i++) { 
                    arr[i] = pairs[i].get("text");
                }

                res.send(arr);
                ...
}

Returns a list of tweets, not just the pointers to the tweets.

[
"I can feel the collingwood jealousy haha",
"@Real_Liam_Payne Now you know how i feel",
"Oh good, I feel much better, after wasting what was meant to be a study day. Classic Charle.",
"RT @NatalieTosh: Almost feel as nervous as if I was watching my own team. #ALeagueFinals #GoRoar",
"@BronB28 Feel the warmth of the ground All roads lead to us around Through endless sunsets and towns I can feel it sitting down here"
]

see: https://www.parse.com/questions/javascriptjquery-pointer

Upvotes: 6

Related Questions