Server crashing on HTTP.call with Meteor + mismatched async results

后端 未结 2 640
一个人的身影
一个人的身影 2021-01-15 19:46

I\'m getting two very weird results trying to make some basic GET calls with Meteor\'s HTTP library.

These same requests work fine with Curl and python, so it\'s not

相关标签:
2条回答
  • 2021-01-15 20:22

    Confusing sync and async (particularly with HTTP) is a really common mistake with meteor. Here's the rule: If you want to get the result back out of the method, use a synchronous call. See this section of the docs for an example. Your implementation should look something like:

    Meteor.methods({
      getEmails: function (authId, threadId) {
        check(authId, String);
        check(threadId, String);
    
        try {
          // note that options is a single object
          var options = {auth: authId, params: {id:threadId};
          var result = HTTP.get("https://api.nylas.com/threads", options);
          // this is just a guess - you should log the result to find
          // out what parts you need to extract
          return result.data;
        } catch (e) {
          // something bad happened
          return false;
        }
    }});
    

    On the client you'll do something like this:

    Meteor.call('getEmails', authId, threadId, function(err, result) {
      return console.log(result);
    });
    

    Note:

    1. options must be a single argument. You were passing two values for options, but the HTTP.get methods assumes it's 3rd argument is a callback. In your case it was an object, and thus the errors.
    2. According to the meteor docs auth doesn't belong in params -- so that's what's causing the 401 error in your Basic HTTP Authentication.
    3. This implementation only works on the server, so place it under your server directory or wrap it in an isServer block.
    0 讨论(0)
  • 2021-01-15 20:40

    Your API request is receiving a 401 unauthorized challenge response.

    You may need to proxy your request to avoid the challenge.

    0 讨论(0)
提交回复
热议问题