Node.js Callback Function Value Return

Asked 2 years ago, Updated 2 years ago, 39 views

Hello.

We are developing using Node.js.

But it's simpler than you think In the face of the problem, I left a question like this.

The code is approximately as follows:

What I want to do is call the sms function, and then the request function is called once more

I'd like to return the value of body that I receive as a print here.

I tried it myself, but undefined pops up.

How can we solve this problem?

After a little googling, I think there's an answer: Hand over the function to the factor and write logic there.

logic there."

I tried it like this answer, but I couldn't return the value.

How can we address these issues?

const response = wait ms (content, body.tel);
function sms (content, recipient) {
    const options = {
        url: 'https://api-sms.cloud.toast.com/sms/v2.1/appKeys/key/sender/sms',
        method: 'POST',
        headers: headers,
        body: JSON.stringify(data)
    };

    request(options).then((body) => {
        console.log(body);
    }).catch((err) => {
        console.log(err);
    });
}

node.js javascript

2022-09-22 18:23

2 Answers

I went in on the url

<StandardResponse>
<header>
<isSuccessful>false</isSuccessful>
<resultCode>-1000</resultCode>
<resultMessage>Invalid appKey.</resultMessage>
</header>
<body/>
</StandardResponse>

There's nothing on the body. That's why undefined appears.


2022-09-22 18:23

async function sms(content, recipient){
    const options = {
        url: 'https://api-sms.cloud.toast.com/sms/v2.1/appKeys/key/sender/sms',
        method: 'POST',
        headers: headers,
        body: JSON.stringify(data)
    };

    return await request(options);
}


2022-09-22 18:23

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.