Link to home
Start Free TrialLog in
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
Avatar of Member_2_7966113
Member_2_7966113

ASKER

Hi I forgot the image

User generated image
Avatar of Leonidas Dosas
Could you post the
console.log(req.body);

Open in new window

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

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

Thanks,

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

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

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

Open in new window

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

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

User generated image
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

Hi Julian,

Thanks again. Going to try your suggestion now..
Hi julian,

I got the following error:

User generated image
Wrong image.. see below

User generated image
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.
Julian,

The attached image shows the options available in HTTP method

User generated image
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.
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:
User generated image
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
So, I used GET and still no luck

User generated image
I read from this Docs Azure
ASKER CERTIFIED SOLUTION
Avatar of Julian Hansen
Julian Hansen
Flag of South Africa image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
oh yeah, I successfully managed to implement the sample from that link
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.
Hi Julian,

I'm trying to figure out how to add the parameter into the request body after entering the ID + 1
User generated image
Tried entering all the parameters, but still can't get the parameters added to the request body..

User generated image
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

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

User generated image
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.
Leonidas,

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

SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
I got the same that you had it...
User generated imageI think that the blank page without errors is a good sign :)
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:

User generated image
I GOT IT

I missed out the { }

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

Cheers
Did it work the url settings as I mentioned above?
Hi Leonidas,

Did it work the url settings as I mentioned above?

Unfortunately not..

If you try, nothing will come back
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.
Thanks guys
You are welcome.