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
LVL 1
Member_2_7966113Asked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Member_2_7966113Author Commented:
Hi I forgot the image

java
0
Leonidas DosasCommented:
Could you post the
console.log(req.body);

Open in new window

0
Member_2_7966113Author 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

0
Determine the Perfect Price for Your IT Services

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

Leonidas DosasCommented:
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

0
Member_2_7966113Author Commented:
Thanks,

Will try that now...
0
Member_2_7966113Author 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

0
Member_2_7966113Author 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

0
Leonidas DosasCommented:
Set pls the
context.log(req.body);

Open in new window

instead of console.log and post the output
0
Member_2_7966113Author 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

0
Member_2_7966113Author Commented:
Any more thoughts?
0
Julian HansenCommented:
It looks like the posted vars are not being converted - have you added a urldecoder to your app?
0
Member_2_7966113Author 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
0
Julian HansenCommented:
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

0
Member_2_7966113Author Commented:
Hi Julian,

Thanks again. Going to try your suggestion now..
0
Member_2_7966113Author Commented:
Hi julian,

I got the following error:

java
0
Member_2_7966113Author Commented:
Wrong image.. see below

java
0
Julian HansenCommented:
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.
0
Member_2_7966113Author Commented:
Julian,

The attached image shows the options available in HTTP method

java
0
Julian HansenCommented:
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.
0
Leonidas DosasCommented:
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
0
Member_2_7966113Author 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
0
Member_2_7966113Author Commented:
So, I used GET and still no luck

java
0
Leonidas DosasCommented:
I read from this Docs Azure
0
Julian HansenCommented:
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
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Member_2_7966113Author Commented:
oh yeah, I successfully managed to implement the sample from that link
0
Member_2_7966113Author 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.
0
Member_2_7966113Author Commented:
Hi Julian,

I'm trying to figure out how to add the parameter into the request body after entering the ID + 1
java
0
Member_2_7966113Author Commented:
Tried entering all the parameters, but still can't get the parameters added to the request body..

java
0
Leonidas DosasCommented:
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

0
Member_2_7966113Author 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
0
Member_2_7966113Author 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.
0
Member_2_7966113Author Commented:
Leonidas,

Do you know how to use the Add Parameter option?
0
Member_2_7966113Author Commented:
Any of you guys still there to help?
0
Leonidas DosasCommented:
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

0
Member_2_7966113Author Commented:
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
0
Leonidas DosasCommented:
I got the same that you had it...
Capture.JPGI think that the blank page without errors is a good sign :)
0
Member_2_7966113Author 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
0
Member_2_7966113Author Commented:
I GOT IT

I missed out the { }

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

Cheers
0
Leonidas DosasCommented:
Did it work the url settings as I mentioned above?
0
Member_2_7966113Author Commented:
Hi Leonidas,

Did it work the url settings as I mentioned above?

Unfortunately not..

If you try, nothing will come back
0
Julian HansenCommented:
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.
0
Member_2_7966113Author Commented:
Thanks guys
0
Julian HansenCommented:
You are welcome.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
JavaScript

From novice to tech pro — start learning today.