I'm trying to read some parameters in the GET method from my custom API, although Azure Mobile Services is always returning this error:
Error in script '/api/customapi.js'. TypeError: Cannot read property 'lat' of undefined at C:\DWASFiles\Sites\XXXXXXXXX-android\VirtualDirectory0\site\wwwroot\App_Data\config\scripts\api\customapi.js:17:49 [external code]
The requested URL is like this: https://XXXXXXXXXX-android.azure-mobile.net/api/customapi?lat=-19&lng=-43
exports.post = function(request, response) {
// Use "request.service" to access features of your mobile service, e.g.:
// var tables = request.service.tables;
// var push = request.service.push;
response.send(statusCodes.OK, { message : 'Hello World!' });
};
exports.get = function(request, response) {
if (request.parameters !== null) {
var req = require("request");
console.log("lat: " + request.parameters.lat);
console.log("lng: " + request.parameters.lng);
}
};
In custom APIs, the request parameter passed has the same properties as the request object used in express.js. To access the query string, you'd use request.query:
exports.get = function(request, response) {
var httpReq = require("request");
var lat = request.query.lat; // the type of the variable is a string; use something like parseFloat if you want it as a number
var lng = request.query.lng;
response.send(200, { lat: lat, lng: lng });
};
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With