Javascript not executing in Azure

Member_2_7966113
Member_2_7966113 used Ask the Experts™
on
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
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®

Author

Commented:
Hi I forgot the image

java
Distinguished Expert 2017

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

Open in new window

Author

Commented:
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

Success in ‘20 With a Profitable Pricing Strategy

Do you wonder if your IT business is truly profitable or if you should raise your prices? Learn how to calculate your overhead burden using our free interactive tool and use it to determine the right price for your IT services. Start calculating Now!

Distinguished Expert 2017

Commented:
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

Author

Commented:
Thanks,

Will try that now...

Author

Commented:
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

Author

Commented:
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

Distinguished Expert 2017

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

Open in new window

instead of console.log and post the output

Author

Commented:
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

Author

Commented:
Any more thoughts?
Most Valuable Expert 2017
Distinguished Expert 2018

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

Author

Commented:
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
Most Valuable Expert 2017
Distinguished Expert 2018

Commented:
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

Author

Commented:
Hi Julian,

Thanks again. Going to try your suggestion now..

Author

Commented:
Hi julian,

I got the following error:

java

Author

Commented:
Wrong image.. see below

java
Most Valuable Expert 2017
Distinguished Expert 2018

Commented:
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.

Author

Commented:
Julian,

The attached image shows the options available in HTTP method

java
Most Valuable Expert 2017
Distinguished Expert 2018

Commented:
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.
Distinguished Expert 2017

Commented:
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

Author

Commented:
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

Author

Commented:
So, I used GET and still no luck

java
Distinguished Expert 2017

Commented:
I read from this Docs Azure
Most Valuable Expert 2017
Distinguished Expert 2018
Commented:
GET uses req.query - not req.body - the problem is the url parameters are not being parsed
When you are entering your POST test data are you putting CR/LF after each line or is the Test container doing that?

Your data should look like this
id=1&name=Carlton&age=23&address=London

Open in new window

Not like this
id=1&
name=Carlton&
age=23&
address=London

Open in new window

Also,try using the Add Parameter option instead of typing into the Request Body section.
Click +Add Paramter
Enter id for key and 1 for the value - repeat for the other values.
Try again

Author

Commented:
oh yeah, I successfully managed to implement the sample from that link

Author

Commented:
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.

Author

Commented:
Hi Julian,

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

Author

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

java
Distinguished Expert 2017

Commented:
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

Author

Commented:
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

Author

Commented:
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.

Author

Commented:
Leonidas,

Do you know how to use the Add Parameter option?

Author

Commented:
Any of you guys still there to help?
Distinguished Expert 2017

Commented:
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

Hi Leonidas,

What did you get when you entered the url? I got a blank. Whereas I got an output when I entered:

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

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

Author

Commented:
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

Author

Commented:
I GOT IT

I missed out the { }

java

Author

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

Cheers
Distinguished Expert 2017

Commented:
Did it work the url settings as I mentioned above?

Author

Commented:
Hi Leonidas,

Did it work the url settings as I mentioned above?

Unfortunately not..

If you try, nothing will come back
Most Valuable Expert 2017
Distinguished Expert 2018

Commented:
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.

Author

Commented:
Thanks guys
Most Valuable Expert 2017
Distinguished Expert 2018

Commented:
You are welcome.

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial