最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

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

programmeradmin1浏览0评论

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 inpetency 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 pleteness)

Cheers.

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 inpetency 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 pleteness)

Cheers.

Share Improve this question edited Apr 27, 2014 at 23:59 mjw asked Apr 27, 2014 at 23:16 mjwmjw 9141 gold badge8 silver badges15 bronze badges 6
  • What? I have not idea what you are asking – Dalorzo Commented Apr 27, 2014 at 23:18
  • see edit. hopefully that's clearer. – mjw Commented Apr 27, 2014 at 23:26
  • Nope, totally unclear, show us the results or the object you are talking about hopefully that would help us understand – Dalorzo Commented Apr 27, 2014 at 23:34
  • Would help to give an example of the response you get and the desired result – juvian Commented Apr 27, 2014 at 23:41
  • results, desired results and further explanation added - sorry for the confusion. I know what my problem is but I'm not that great at articulating it via text alone. – mjw Commented Apr 28, 2014 at 0:01
 |  Show 1 more ment

1 Answer 1

Reset to default 6

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./questions/javascriptjquery-pointer

发布评论

评论列表(0)

  1. 暂无评论