Avatar of Member_2_7966113
Member_2_7966113
 asked on

Javascript not executing in Azure

Hello Experts,

I have coped and pasted the following javascript into my application, see image, but I keep on getting the error message shown in the image.
module.exports = function (context, req) {
    context.log('JavaScript HTTP trigger function processed a request.');
    if (req.body && req.body.name && req.body.id && req.body.age && req.body.address){

    context.res = {
        //status: 200, /* Defaults to 200 */
        body: "Data is saved for user " + (req.body.name) +" !!"
    };

     context.bindings.outputDocument = JSON.stringify({
         id: req.body.id,
         name: req.body.name,
         age: req.body.age,
         address: req.body.address
     });
}
else {
    context.res = {
        status: 400,
        body: "Please pass all user details"
    };
}
context.done();

};

Open in new window

Can someone let me know where I'm going wrong?

Thanks

Carlton
JavaScriptAzure

Avatar of undefined
Last Comment
Julian Hansen

8/22/2022 - Mon
Member_2_7966113

ASKER
Hi I forgot the image

java
Leonidas Dosas

Could you post the
console.log(req.body);

Open in new window

Member_2_7966113

ASKER
Hi Leonidas,

Thanks for responding. I'm 100% new with Javascript.

Can you let me know how to post the
console.log(req.body):

Open in new window

Experts Exchange is like having an extremely knowledgeable team sitting and waiting for your call. Couldn't do my job half as well as I do without it!
James Murphy
Leonidas Dosas

Add into your code the console.log as I show below and then check your console.
module.exports = function (context, req) {
    context.log('JavaScript HTTP trigger function processed a request.');
    console.log(req.body);
    if (req.body && req.body.name && req.body.id && req.body.age && req.body.address){

    context.res = {
        //status: 200, /* Defaults to 200 */
        body: "Data is saved for user " + (req.body.name) +" !!"
    };

     context.bindings.outputDocument = JSON.stringify({
         id: req.body.id,
         name: req.body.name,
         age: req.body.age,
         address: req.body.address
     });
}
else {
    context.res = {
        status: 400,
        body: "Please pass all user details"
    };
}
context.done();

};

Open in new window

One of the if statement parameter is not pass the true statement.Also try to change the if statement with this:
 if ( req.body.name && req.body.id && req.body.age && req.body.address){
//code here...
}

Open in new window

Member_2_7966113

ASKER
Thanks,

Will try that now...
Member_2_7966113

ASKER
Hi still getting the same error

module.exports = function (context, req) {
    context.log('JavaScript HTTP trigger function processed a request.');
    console.log(req.body);
    if (req.body && req.body.name && req.body.id && req.body.age && req.body.address){
    context.res = {
        //status: 200, /* Defaults to 200 */
        body: "Data is saved for user " + (req.body.name) +" !!"
    };

     context.bindings.outputDocument = JSON.stringify({
         id: req.body.id,
         name: req.body.name,
         age: req.body.age,
         address: req.body.address
     });
}
else {
    context.res = {
        status: 400,
        body: "Please pass all user details"
    };
}
context.done();

};

Open in new window

⚡ FREE TRIAL OFFER
Try out a week of full access for free.
Find out why thousands trust the EE community with their toughest problems.
Member_2_7966113

ASKER
This also failed:

module.exports = function (context, req) {
    context.log('JavaScript HTTP trigger function processed a request.');
    console.log(req.body);
    if ( req.body.name && req.body.id && req.body.age && req.body.address){
    context.res = {
        //status: 200, /* Defaults to 200 */
        body: "Data is saved for user " + (req.body.name) +" !!"
    };

     context.bindings.outputDocument = JSON.stringify({
         id: req.body.id,
         name: req.body.name,
         age: req.body.age,
         address: req.body.address
     });
}
else {
    context.res = {
        status: 400,
        body: "Please pass all user details"
    };
}
context.done();

};

Open in new window

Leonidas Dosas

Set pls the
context.log(req.body);

Open in new window

instead of console.log and post the output
Member_2_7966113

ASKER
Hi, after changing to context, this is the output

2017-11-18T09:55:17  Welcome, you are now connected to log-streaming service.
2017-11-18T09:55:29.396 Script for function 'SaveDataAPI' changed. Reloading.
2017-11-18T09:55:30.364 Function started (Id=a1d7e131-943c-4cd2-ad00-0fd88ecbe6f9)
2017-11-18T09:55:30.378 JavaScript HTTP trigger function processed a request.
2017-11-18T09:55:30.378 "id":"1",
"name":"Carlton",
"age":"4",
"address":"London"
2017-11-18T09:55:30.378 Function completed (Success, Id=xxxxxxxxxxxxxxxxxxxxxxx, Duration=24ms)

Open in new window

I started with Experts Exchange in 2004 and it's been a mainstay of my professional computing life since. It helped me launch a career as a programmer / Oracle data analyst
William Peck
Member_2_7966113

ASKER
Any more thoughts?
Julian Hansen

It looks like the posted vars are not being converted - have you added a urldecoder to your app?
Member_2_7966113

ASKER
Hi Julian,

Thanks for responding.

I'm not sure about vars, however below is the out after I run the script with the test variables

"id":"1",
"name": "Carlton",
"age":"23",
"address":"London"

java
⚡ FREE TRIAL OFFER
Try out a week of full access for free.
Find out why thousands trust the EE community with their toughest problems.
Julian Hansen

Ok but I would expect to see something like this from your dump of req.body
{ id: '1', age: '2', address: 'a', name: 'b' }

Open in new window


I think I see your problem though
You are doing a POST but defining your data as if it was JSON
In your Test window try this
id=1&name=Carlton&age=23&address=London

Open in new window

Member_2_7966113

ASKER
Hi Julian,

Thanks again. Going to try your suggestion now..
Member_2_7966113

ASKER
Hi julian,

I got the following error:

java
Your help has saved me hundreds of hours of internet surfing.
fblack61
Member_2_7966113

ASKER
Wrong image.. see below

java
Julian Hansen

Ok then this is telling me that there is no server process that is processing the POST body - what we are seeing there is the raw post data - whereas what we want to be seeing is an object.

I am not familiar with the Azure platform - but somewhere in your app you need to be telling it that you are expecting URLEncoded data so that it knows how to decode it.
Are there any other files in your project.
Member_2_7966113

ASKER
Julian,

The attached image shows the options available in HTTP method

java
⚡ FREE TRIAL OFFER
Try out a week of full access for free.
Find out why thousands trust the EE community with their toughest problems.
Julian Hansen

Thanks but that is not where the problem is. The test container just defines how you send your data to the backend service.

The problem here is on how the service is interpreting that data.

Is this your only app file or are there others listed behind View Files

Somewhere you must define how to interpret the incoming data.
Leonidas Dosas

Change the method to GET and retry. The key-values pairs are in the url and you must get them from this as I show at this documentation:
Capture.JPG
Member_2_7966113

ASKER
oh wow, Leonidas are you reading from the same book located here?

https://www.packtpub.com/mapt/book/virtualization_and_cloud/9781787122932/6/ch06lvl1sec32/building-the-project

I will try your suggestion

Cheers
This is the best money I have ever spent. I cannot not tell you how many times these folks have saved my bacon. I learn so much from the contributors.
rwheeler23
Member_2_7966113

ASKER
So, I used GET and still no luck

java
Leonidas Dosas

I read from this Docs Azure
ASKER CERTIFIED SOLUTION
Julian Hansen

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
Member_2_7966113

ASKER
oh yeah, I successfully managed to implement the sample from that link
⚡ FREE TRIAL OFFER
Try out a week of full access for free.
Find out why thousands trust the EE community with their toughest problems.
Member_2_7966113

ASKER
ahhhh  Julian, I think you might onto something there. I wil try and use the 'Add Parameter' option.

I will let you know how I get on.
Member_2_7966113

ASKER
Hi Julian,

I'm trying to figure out how to add the parameter into the request body after entering the ID + 1
java
Member_2_7966113

ASKER
Tried entering all the parameters, but still can't get the parameters added to the request body..

java
All of life is about relationships, and EE has made a viirtual community a real community. It lifts everyone's boat
William Peck
Leonidas Dosas

Get the url and add these parameter ass I posted above:
https//:some_url_here.net/api/HttpTrigger..etc?id=1&name=Carlton&age=23&address=London

Open in new window

Member_2_7966113

ASKER
Hi Leonidas,

I tried adding the parameter to the url as you suggested but it didn't work. However, adding the string as follows got a result but not the right result

https://projectfunction.azurewebsites.net/api/SaveDataAPI?code=aTqhABAA3TMKEhNRUQSawJnHFHjIUgiL/M8HW/0K4VkYwNk94n5G0g==&id=1&name=Carlton&age=23&address=London

java
Member_2_7966113

ASKER
Julian,

I added the parameters as you suggested. I'm still don't know what command to enter to get the parameters into the 'Request body' when I use the 'Add parameter' option.
⚡ FREE TRIAL OFFER
Try out a week of full access for free.
Find out why thousands trust the EE community with their toughest problems.
Member_2_7966113

ASKER
Leonidas,

Do you know how to use the Add Parameter option?
Member_2_7966113

ASKER
Any of you guys still there to help?
Leonidas Dosas

Try with this:
https://projectfunction.azurewebsites.net/api/SaveDataAPI?code=aTqhABAA3TMKEhNRUQSawJnHFHjIUgiL/M8HW/0K4VkYwNk94n5G0g==?id=1&name=Carlton&age=23&address=London

Open in new window

Experts Exchange has (a) saved my job multiple times, (b) saved me hours, days, and even weeks of work, and often (c) makes me look like a superhero! This place is MAGIC!
Walt Forbes
SOLUTION
Member_2_7966113

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
⚡ FREE TRIAL OFFER
Try out a week of full access for free.
Find out why thousands trust the EE community with their toughest problems.
Leonidas Dosas

I got the same that you had it...
Capture.JPGI think that the blank page without errors is a good sign :)
Member_2_7966113

ASKER
The problem is, with your url I don't see any activity when I enter it in the browser. Whereas, although I see the error message with my url I see it being entered in my log when I enter it in my browser:

java
Member_2_7966113

ASKER
⚡ FREE TRIAL OFFER
Try out a week of full access for free.
Find out why thousands trust the EE community with their toughest problems.
Member_2_7966113

ASKER
Member_2_7966113

ASKER
I GOT IT

I missed out the { }

java
Member_2_7966113

ASKER
If someone knows how to add a parameter option as Julian mentioned please let me know

Cheers
Experts Exchange is like having an extremely knowledgeable team sitting and waiting for your call. Couldn't do my job half as well as I do without it!
James Murphy
Leonidas Dosas

Did it work the url settings as I mentioned above?
Member_2_7966113

ASKER
Hi Leonidas,

Did it work the url settings as I mentioned above?

Unfortunately not..

If you try, nothing will come back
Julian Hansen

Adding parameters - have not tried but should be a case of entering the values and they are added to the Request body

Missing { } => your code is expecting a JSON post not a urlencoded - I would need to get some more experience points on Azure for being able to say why - but for now it looks like the Request Body needs a valid JSON string to work.
⚡ FREE TRIAL OFFER
Try out a week of full access for free.
Find out why thousands trust the EE community with their toughest problems.
Member_2_7966113

ASKER
Thanks guys
Julian Hansen

You are welcome.