0

I've been able to fetch the data from the database, but then i'm trying to make use of it but it's not working,

var a = function() {
  first
    .find(
      { url: "https://guarded-everglades-31972.herokuapp.com/getCalls" },
      { url: 1 }
    )
    .then(url => {
      if (!url) {
        return console.log("url not found");
      }
      console.log("Todo by id", url);
    })
    .catch(e => console.log(e));
};

var accountSid = "…";
var authToken = "…";

var client = require("twilio")(accountSid, authToken);

client.calls.create(
  {
    url: a(),
    to: "+2348033260017",
    from: "+1 714 361 9371"
  },
  function(err, call) {
    if (err) {
      console.log(err);
    } else {
      console.log(call.sid);
    }
  }
);

I'm trying to use the fetched url from the function as the url for the twilio app, but it's telling me that i need to provide a url.

nem035
  • 34,790
  • 6
  • 87
  • 99

1 Answers1

0

You are looking up the URL in an asynchronous callback function:

url => {
  if (!url) {
    return console.log("url not found");
  }
    console.log("Todo by id", url);
}

Because of this, your call to a() doesn't return the url as you might expect:

client.calls.create(
{
  url: a(), <-- here is the problem
  to: "+2348033260017",
  from: "+1 714 361 9371"
},

Instead, you want to use the url inside the callback function. Something like this:

var accountSid = "…";
var authToken = "…";

var client = require("twilio")(accountSid, authToken);

var a = function() {
  first.find(
      { url: "https://guarded-everglades-31972.herokuapp.com/getCalls" },
      { url: 1 })
    .then(url => {
      if (url) {
        client.calls.create({
          url,
          to: "+2348033260017",
          from: "+1 714 361 9371"
        },
        function(err, call) {
          if (err) {
            console.log(err);
          } else {
            console.log(call.sid);
          }
        });
      }
    });
};
Alex McMillan
  • 17,096
  • 12
  • 55
  • 88