I am running into an error I believe with calling ...
# orm-help
j
I am running into an error I believe with calling a third party api in my resolver. Here is the code:
Copy code
const getPackage = async (parent, args, context) => {
  const package = await context.prisma.package({ date: args.date });
  // If package exist return it, else, create new package
  if (package) {
    return package;
  } else {
    let pack = await context.prisma.createPackage({
      date: args.date,
      // Request Advice(error here - returns undefined/null)
      advice: getAdvice(),
      // Request Picture(pending - will use a third party API)
      picture: `pic_${args.date}.jpg`,
      // defaults empty array
      comments: { set: [] }
    });
    return pack;
  }
};
Here is my
getAdvice()
function from `Helper.js`:
Copy code
const getAdvice = () => {
  // Get advice
  axios
    .get('<https://api.adviceslip.com/advice>')
    .then(res => {
      return res.data.slip.advice;
    })
    .catch(err => {
      console.log(err);
      return null;
    });
};

module.exports = {
  getAdvice
};
Here is my error:
Copy code
{
  "data": null,
  "errors": [
    {
      "message": "Variable '$data' expected value of type 'PackageCreateInput!' but got: {\"date\":\"2019-4-24\",\"picture\":\"pic_2019-4-24.jpg\",\"comments\":{\"set\":[]}}. Reason: 'advice' Expected non-null value, found null. (line 1, column 11):\nmutation ($data: PackageCreateInput!) {\n          ^",
      "locations": [
        {
          "line": 2,
          "column": 3
        }
      ],
      "path": [
        "getPackage"
      ]
    }
  ]
}
Any help would be great. I feel like I cant be to far off.
b
- does
getAdvice()
return
null
? If so, the error is expected because
advice
is required, it can't be null, - I think it should be
Copy code
advice: await getAdvice()
j
@bkstorm
getAdvice()
returns null only if there is an error but it should also console log the error, which its not happening. its supposed to return a string which is the advice.
I tried adding the await but that didn’t work either.
If I put a console log in my
axios.then
it prints out the advice so I know its going to the
.then
not the
.catch
so it shouldn’t be returning
null
.
b
@Jameson Brown You didn't return promise in
getAdvice()
, so the function return nothing. Just add
return
before
axios
, then add
await
before
getAdvice()
j
@bkstorm you are a genius. I knew it wasn’t anything to big. Its working now. Thanks!
👍 1