0

I'm suffering to create a server requesting to external server.

What I'm planning to do here is, sending a request to external server and return a code(like success or fail) to the client. But the callback function doesn't return. How should I make this happen?


app.post('/request', (req, res) =>{
    const value = 'blah blah cyka'
    const uploadValue = uploadTo(value)
    
    res.json(uploadValue)
    return
}


// This is request function
function uploadTo(VALUE){

    await request.post({
        url: 'https://stackunderpants.com/api',
        method: 'POST',
        rejectUnauthorized: false, 
        headers: {
            'Content-Type': 'application/json',
            'Accept': 'application/json'
        },
        body: JSON.stringify(VALUE)
    }, (err, response, body) =>{
        if(err){
            console.log(response)
            console.log(body)
            console.log('Error has been occurred while sending to server. \n', err)
            return {
                'code': '400',
                'detail': 'Error has been occurred while sending to server'
            }
        }

        return {
            'code': '200'
        }
    })

}

I've tried await async but not working...

const uploadValue = await uploadTo(value)

I'm literally dying right now.. it has been a week since I got this

Yisub Heo
  • 1
  • 1
  • 1
    Does this answer your question? [How do I return the response from an asynchronous call?](https://stackoverflow.com/questions/14220321/how-do-i-return-the-response-from-an-asynchronous-call) – Nick Parsons Dec 07 '20 at 07:46
  • 1
    umm.. It's hard.. but I'll try and let you know how it goes ^^ thx – Yisub Heo Dec 07 '20 at 08:14
  • Have a look at https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Using_promises and https://javascript.info/async-await. Promisify the uploadTo function, then you can use const uploadValue = await uploadTo(value) – kg99 Dec 07 '20 at 09:02
  • since ```request.post({ ``` is not a function, promise is not available.. Am I missing something? – Yisub Heo Dec 07 '20 at 09:08
  • nvm. I somehow manage to solve it with ```axios``` instead of ```request``` thanks for the help^^ – Yisub Heo Dec 07 '20 at 09:15

1 Answers1

2

Just edit your 3 lines code:

  1. add "async" in app.post('/request', async (req, res) =>{...
  2. add "await" in const uploadValue = await uploadTo(value) ...
  3. add "await" in async function uploadTo(VALUE){ ....

`app.post('/request', async (req, res) =>{

const value = 'blah blah cyka'
const uploadValue = await uploadTo(value)
res.json(uploadValue)
return

}`

// This is request function

`async function uploadTo(VALUE){

await request.post({
    url: 'https://stackunderpants.com/api',
    method: 'POST',
    rejectUnauthorized: false, 
    headers: {
        'Content-Type': 'application/json',
        'Accept': 'application/json'
    },
    body: JSON.stringify(VALUE)
}, (err, response, body) =>{
    if(err){
        console.log(response)
        console.log(body)
        console.log('Error has been occurred while sending to server. \n', err)
        return {
            'code': '400',
            'detail': 'Error has been occurred while sending to server'
        }
    }

    return {
        'code': '200'
    }
})

}`

Debasis Das
  • 549
  • 3
  • 8