0

I'm attempting to insert an API response into my mongo db, but it doesn't save the individual fields.

Input here

PlayersList.insert(Meteor.http.get("API CALL HERE"));

Here is the response from the call:

{"hnub": {
   "id": 21098134,
   "name": "Hnub",
   "profileIconId": 20,
   "revisionDate": 1428613578000,
   "summonerLevel": 30
}}

Is there a simple way to do this cleanly saving all the fields?

1
  • Something like PlayersList.insert(Meteor.http.get("API CALL HERE")["hnub"]);? Commented Apr 9, 2015 at 21:57

2 Answers 2

1

You should do the inset into the callback not like that.

check the syntaxis HTTP.get(url, [callOptions], [asyncCallback])

Callback that is called when the request is completed. Required on the client.

from docs.

So try with.

Meteor.http.get(url,function(err,result){
 if(!err){
  PlayersList.insert({result})
  }
})

Be sure you have $ meteor add http, if this don't work for some Access-Control-Allow-Origin policy use Meteor.method/Meteor.call

Sign up to request clarification or add additional context in comments.

Comments

0

The response of an HTTP call is an object with content (String), data (JavaScript Object if it can be parsed as JSON), headers, and statusCode. I recommend that you run some code like this to see the actual response:

console.log(Meteor.HTTP.get("API CALL HERE"));

If it turns out that response includes parsed JSON under the data field, then you could accomplish what you want with:

PlayersList.insert(Meteor.HTTP.get("API CALL HERE").data);

Note, this synchronous way of making an API call will only work from the server. As @Ethaan points out, you'll need to use a callback to do it from the client and you'll have to overcome cross domain restrictions.

1 Comment

Thank you very much I was performing this on the server so .data works well

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.