0

I'm working on 2 steps authentication with nexmo, I have a plan to realize this by graphl mutation because I have graphql API and I can't get to requestId value

  Mutation: {
    signUpFirstStep: async ( parent, { number }, { models, secret }) => 
    {
     const response =  nexmo.verify.request({
        number: number,
        brand: 'Nexmo',
        code_length: '4'
      }, (err, result) => {
        const  requestId  = result.request_id
        return requestId

      });
      console.log(response);  //right here I have undefined
  }
}

All I want is to get requestId value to return it in the mutation

Robert
  • 9
  • 3
  • First, you are referencing `response` outside of its scope, hence `undefined`. Second you are dealing with an asynchronous function, the response is either an error or the result. Have you logged `err` and `result`? – JDunken Oct 18 '19 at 15:02
  • @JDunken yes, ofc but i don't know how to get value out of the scope – Robert Oct 18 '19 at 15:05
  • Declare the variable outside of the function and set it in the callback if there is a result. – JDunken Oct 18 '19 at 15:07
  • Please be mindful that GraphQL.js relies on Promises and you should not mix Promises and callbacks. If the `nexmo` library doesn't support Promises, you should wrap the callback inside a Promise. See Common Scenario #6 [here](https://stackoverflow.com/a/56319138/6024220). – Daniel Rearden Oct 18 '19 at 15:08
  • @DanielRearden thanks for a response, it helped ;) – Robert Oct 18 '19 at 18:24

1 Answers1

1

nexmo.verify.request isn't going to return what you want. You need to console.log, or handle requestId however you like, inside of (err, result) => {} You could also potentially do res.status(200).send(result); or res.status(200).send(requestId); to respond back with the result or requestId, if you would like.

See Below:

Mutation: {
    signUpFirstStep: async ( parent, { number }, { models, secret }) => 
    {
     nexmo.verify.request({
        number: number,
        brand: 'Nexmo',
        code_length: '4'
      }, (err, result) => {

        if(result) {  
          const  requestId  = result.request_id;
          console.log(requestId); // you should console.log or do whatever you are trying to do with the requestId here
        }
      });

  }
}
Jonathan Irvin
  • 896
  • 8
  • 17
  • This is a GraphQL resolver, not an express route controller. Even if you have access to the `res` object through the context, you wouldn't use it to return any data. – Daniel Rearden Oct 18 '19 at 15:28