简体   繁体   中英

Unable to retrieve included objects from Parse.Query

I'm having an issue regarding Parse.Query and how to break down the result promise it. This is currently my code:

var ga = new Parse.Query(Game);
ga.include('away_team');
ga.include('home_team');
ga.equalTo("objectId", req.params.id);
ga.find().then(function(result){
  console.log(result[0]);
});

That console log outputs something like this:

{
  "home_team" : {"name":"Dallas Stars", "arena":"American Airlines Center"},
  "away_team" : {"name":"New York Rangers", "arena":"Madison Square Garden"},
  "createdAt" : "2015-12-28T10:35:35.541Z"
}

My problem is, how can I get the name from away_team inside my promise function? If I change the console.log line to console.log(results[0].createdAt) . My console outputs
2015-12-28T10:35:35.541Z as intended. But if I change that line further to console.log(results[0].away_team) my console logs No message provided .

Which apparently is wrong, in my world (inside my head) it would've returned {"name":"New York Rangers", "arena":"Madison Square Garden"}

But okay, fine, I don't really need that line but what I need is to retrieve the name from it. When I try the following line: console.log(results[0].away_team.name)
I get an error saying

I can do this (results.away_team.name) within my .jade template if I send it to view with JSON.serialize() and in my view parse it with JSON.parse(). If I try doing this in my router class, I get either or 会收到会得到

My goal is to send a correct title to my view depending on the home and away team in the current game I am browsing like below:

res.render('inside/game', {
  title: result[0].away_team.name + " vs. " + result[0].home_team.name
  // title: New York Rangers vs. Dallas Stars
});

Am I trying to work something out that is not intended to work within the routing class?

The result of the find is a parse object, not JSON. Your workaround is taking that object and converting it, via JSON text, to plain JS objects, which isn't great but does work.

Parse objects can't access most content directly because they don't have explicitly defined methods for arbitrary keys. Instead you need to use the get function.

result[0].get("home_team")

And you can chain a subsequent request for the name or store the result to a var so you can unpack multiple pieces of data efficiently.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM