Node.js app receives empty response from GET request to API - javascript

I'm new to node.js so I'll try my best to explain the problem here. Let me know if any clerification is needed.
In my node.js application I'm trying to take a code (which was received from the response of the 1st call to an API), and use that a code to make a 2nd request(GET request) to another API service. The callback url of the 1st call is /pass. However I got an empty response from the service for this 2nd call.
My understanding is that after the call back from the 1st call, the function in app.get('/pass', function (req, res).. gets invoked and it sends a GET request. What am I doing wrong here? Many thanks in advance!
Here is the part where I try to make a GET request from node.js server and receive an empty response:
app.get('/pass', function (req, res){
var options = {
url: 'https://the url that I make GET request to',
method: 'GET',
headers: {
'authorization_code': code,
'Customer-Id':'someID',
'Customer-Secret':'somePassword'
}
};
request(options, function(err, res, body) {
console.log(res);
});
});

Im a little confused by what you are asking so ill just try to cover what i think you're looking for.
app.get('/pass', (req, res) => {
res.send("hello!"); // localhost:port/pass will return hello
})
Now, if you are trying to call a get request from the request library when the /pass endpoint is called things are still similar. First, i think you can remove the 'method' : 'GET' keys and values as they are not necessary. Now the code will be mostly the same as before except for the response.
app.get('/pass', (req, res) => {
var options = {
url: 'https://the url that I make GET request to',
headers: {
'authorization_code': code,
'Customer-Id':'someID',
'Customer-Secret':'somePassword'
}
};
request(options, function(err, res, body) {
// may need to JSONparse the body before sending depending on what is to be expected.
res.send(body); // this sends the data back
});
});

Related

POST http://localhost:3000/updateSurvey 400 (Bad Request)

I am trying to update a collection in mongoDB after the user finishes some tasks. However, whenever I attempt to save the information and update mongo, I'm getting the error POST http://localhost:3000/updateSurvey/634124db6f 400 (Bad Request). Any ideas why my code isn't functioning correctly?
Backend js script
app.post('/updateSurvey', async (req, res) => {
try {
await client.connect();
var db = client.db('Admin_Db');
var collection = db.collection('Survey');
await collection.findOneAndUpdate({"_id": ObjectId(req.body.id)}, {completion: req.body.completion});
res.send("updated");
} catch (error) {
console.log(error);
res.status(400).send(error);
}
});
Main.js (this is how I am fetching the data from mongo)
fetch("http://localhost:3000/updateSurvey", {
method:'POST',
headers:{'Content-Type': 'application/json'},
body: JSON.stringify({id: surveyID, completion: true})})
.then(response=>{response.text()})
.catch(function(err){console.log("Fetch Problem: " + err)});
You don't have a route http://localhost:3000/updateSurvey/634124db6f exposed on your server. Therefore the 404 error. Since you are using a post call, just pass the surveyID in your body when making the post call using fetchAPI instead of sending it as a query param.
And make sure http://localhost:3000/updateSurvey is the route to which your are sending your data, without the surveyId in the URL.
Edit
Edits made as per request received in comments.
collection.findOneAndUpdate({"_id": id)}, {completion: req.body.completion});
should be:
collection.findOneAndUpdate({"_id": new ObjectId(id))}, {completion: req.body.completion});
_id is of type ObjectId in MongoDB. You are passing id as string. This should most likely be the error from what I can gather by the code shared in your question. You can cast a string to ObjectId by using the ObjectId class provided by the Mongo Driver for node. Even mongoose provides this class.

Node request external API within local get request

I'm trying provide the response of an external API call when I perform a local GET request but struggling with how to get this to work.
My code at the moment is:
app.get('/', function(req, res){
request.post('http://data.fixer.io/api/latest?access_key=' + apikey +
'&symbols=gbp', function(err, res, body) {
console.log(body)
})
res.render('index')
})
My knowledge and experience with callbacks and async programming is limited, but how do I pass the response of the request POST into the GET request to then pass it to the index?
Thanks!
Callbacks can be difficult to understand, and the problem you describe isn't uncommon (it even has a name - Callback Hell). Partly the reason why Node introduced the async / await syntax - here's the equivalent of your code in that style
app.get('/', async (req, res, next) => {
try {
const uri = `http://data.fixer.io/api/latest?access_key=${apikey}&symbols=gbp`;
const data = await request.post(uri);
return res.render('index', { data }); // or pass whatever you need from `data` into the view
} catch (e) {
return next(e);
}
}
Notice the one big difference? No callbacks and you get all the same benefits of asynchronous code with bonus of writing code in a synchronous style.
You can chain calls in Express, so it's very easy to call an external service within a GET request, e.g.
app.get('/', function(req, res){
request.post('http://data.fixer.io/api/latest?access_key=' + apikey + '&symbols=gbp', function(err, response, body) {
console.log(body)
res.send(body);
})
})
In this case we're sending back the raw response from the POST, it is easy to wrap this in another object, e.g.
res.send( { status: 'ok', post_result: body });

Angular-Fullstack: Using $resourse query with params

I'm using Angular-Fullstack generator, and I'm not able to get a list of drivers depending on a companyID, through $resource query. This is what I have:
server/api/driver/index.js:
router.get('/:company', controller.index);
server/api/driver/driver.controller.js:
export function index(req, res) {
return Driver.find({company: req.params.company}).exec()
.then(function(res){
console.log(res); /* I get here the result correctly */
respondWithResult(res)
})
.catch(handleError(res));
}
client/services/driver.service.js:
export function DriverResource($resource) {
'ngInject';
return $resource('/api/drivers/:id/:company', {company: '#_id'});
}
client/app/driver/driver.controller.js:
this.driverList = Driver.query({company: Auth.getCurrentUser()._id}});
console.log(this.driverList); /* Empty array */
I'd be grateful if someone could help me getting the response from the server...
Thank you in advance.
I just realised that I was duplicating the 'res' variable:
server/api/driver/driver.controller.js:
export function index(req, res) {
return Driver.find({company: req.params.company}).exec()
.then(function(**res**){
/* Should be the result, not the response */
console.log(**res**);
respondWithResult(**res**)
})
.catch(handleError(res));
}
You were close.
Driver.query({company: 'foo'}).$promise.then(function(results) {
console.log(results) //here
}, function(err) {
//here is your 404 error, but you should create an http interceptor
});
It's async, do you don't get your results right away.
This will work of course, assuming your backend responds properly.
EDIT: Your backend is missing some endpoints. You should be able to respond to requests to /api/drivers/ with a list of drivers
EDIT 2:
Angular's resource will give you access to some methods:
Driver.get(1) Will make a request to /api/drivers/:id and will be expecting the backend to respond with an object representing the driver with said ID. This should be used when you want to fetch only 1 record
Driver.query({foo: 'bar', some_id: 1}) Will make a request to /api/drivers?foo=bar&some_id=1 and will be expecting the backend to respond with an array of objects, each representing a driver. This should be used when you want to fetch several records, for example in an index.
Driver.query() will make a request to /api/drivers and will be expecting the backend to respond with an array
Driver.create(data) will make a POST request to /api/drivers and will expect an object (the created driver) in the response. Used to create a new record
There are some others, this is the ones I use.
So, your backend, considering you are using this three methods, needs to handle:
router.get('/drivers/:id', function(req, res) {
let id = req.params.id
})
router.get('/drivers', function(req, res) {
//if request was /drivers?foo=bar
let foo = req.query.foo
})
router.post('/drivers', function(req, res) {
let body = req.body
})
As I said, there are several things in play here. If you are at a lost, break the problem into pieces. Get the backend working before going to Angular.

Send result from HTTP request in Angular to NodeJS

I am currently sending a request from NodeJS to get some data, as such :
In Angular :
$http.post(API_ENDPOINT.url + '/annonce', {'link': url}).then(function (result) {
...
}
And in Node :
apiRoutes.post('/annonce', function (req, res) {
const url = req.body.link
request.get({
uri: url,
encoding: null
}, function (error, response, html) {
if (!error) {
res.send(parser(url, html))
}
})
return res
})
I would like to send this request from my front-end (Angular). I guess I could simply do the request like this :
$http.get(url).then(function(result)) {
// send another post request to the back end with the result
}
but I've heard it was easier to use pipes in this case.
Thing is, I really don't understand how to make it work. Can anyone help ?
If the page you are trying to retrieve does not have the CORS headers then angular will fail to make the request.
If you expand on your usecase it should be easier to get you some help.

Crossdomian ajax requests to server returned json object shows error Unexpected token :

I am running an express server in which I am checking for get requests to certain url requests. Now in the reply I am trying to send a reply in json format so that I can make ajax calls to the page and get data. Now to make cross domain requsts I had to use jsonp. Now when the request is completed and the data is returned to the callback it shows error Uncaught SyntaxError: Unexpected token :.
Here are the server and client codes below
Server
var express=require('express');
var app = express();
var http = require('http').Server(app);
var port=Number(3000);
var server_ip_address = '127.0.0.1';
http.listen(port,server_ip_address, function(){
console.log('listening on '+port);
});
app.get('/test', function (req, res) {
console.log(req.query);
res.send({message:'hello'});
});
Client
$.ajax({
url: 'http://localhost:3000/test',
type: 'GET',
dataType:'jsonp',
crossDomain:true,
data: {
username: $('#username').val(),
password: $('#pwd').val()
},
success: function(result){
console.log($.parseJSON(result));
},
jsonp:'jsonp'
});
In the server code i have tried using
res.setHeader('Content-Type', 'application/javascript');
res.send({message:'recieved'});
even
res.setHeader('Content-Type', 'application/json');
res.send({message:'recieved'});
In the client i have tried logging the result object as it is but to no avail. Any help is appreciated. Moreover any suggestions to use other methods to fetch data are equally welcome. Thanks in advance.
The simplest way to do this is just use res.jsonp() rather than setting your own headers etc:
app.get('/test', function (req, res) {
console.log(req.query);
res.jsonp({message: 'hello'});
});
The default callback parameter is just callback but you can change it (if you need to) like so:
app.set('jsonp callback name', 'cb');
note: You might have to remove the jsonp:'jsonp' from your client-side code as that assigned the name of the callback. I'd also suggest you swap it from a GET to a POST request if you are submitting data (especially sensitive data).

Categories