How to get all reviews in themoviedb api? - javascript

I am creating a rest api , I would like to retrieve or get all reviews/comments which are in a database.
This is what I have done :
app.get('/review', (req, res) =>{
request('https://api.themoviedb.org/3/review?api_key=4d9c9de3bdf0d3b6837c49c086e3b190', function (error, response, body) {
console.log('error:', error); // Print the error if one occurred and handle it
console.log('statusCode:', response && response.statusCode); // Print the response status code if a response was received
res.send(body)
});
});
I am getting this error:
{"status_code":34,"status_message":"The resource you requested could not be found."}
what am I doing wrong here? help
Note: other methods works perfectly

This isn't a JS error or anything. If you look at the API Documentation, status_code: 34 means you're accessing an endpoint that doesn't exist.
Diving into the docs a bit further, you can't get ALL the reviews in the database. All you can do is get the reviews on a per movie basis. Try this URL:
https://api.themoviedb.org/3/movie/401478/reviews?api_key=4d9c9de3bdf0d3b6837c49c086e3b190
Here's the documentation on the /movie/<movie_id>/reviews endpoint: https://developers.themoviedb.org/3/movies/get-movie-reviews
There is also the /review/<review_id> end point, but it appears that gets a single review by id which probably isn't what you're looking for:
https://developers.themoviedb.org/3/reviews/get-review-details

It has nothing to do with nodeJS.As stated by #VicJordan, the problem is only with the url you are trying to search, it's simply not a valid API request. Try to go thru API documentation to find out how to use them. An example of a valid URL would be:
https://api.themoviedb.org/3/discover/movie?api_key=4d9c9de3bdf0d3b6837c49c086e3b190

Related

Catching Error With Custom Rest API For FireStore Javascript

I have been watching a tutorial on making a Rest API for Firestore which appears to work but I cannot figure out how to catch an error.
The code below basically uses an end point to retrieve a document id from the firestore database.
The client uses javascript fetch to call the API.
I am trying to workout how to return something back to the client from the API if the document id is not there. I thought I might get a 404 status returned but I always get status 200.
This is the API code I used
app.get("/api/read/:id", (req, res) => {
(async () => {
try {
const document = db.collection("users").doc(req.params.id);
let product = await document.get();
let response = product.data();
return res.status(200).send(response);
} catch (error) {
console.log(error);
return res.status(500).send(error);
}
})();
})
I'm fairly certain that the 404 message is for the server itself not being found (though I do need to brush up on my error codes).
However, if you're looking to check to see if a document exists there's a command specifically for that demonstrated in the examples in the firebase docs

Angular 10 subscribe is failing to post with error code OK(and no other errors shown)

I am new to angular 10 and I am trying to make an http post to a PHP file as shown below
this.http.post(`${environment.server}/path/file.php`, {param1, param2})
.subscribe(
data => {
console.log(JSON.stringify(data));
},
error => {
console.log(error);
this.error = error;
});
The file is successfully called and returns the following JSON as displayed in the console response
{"Email":null,"school_year":2021,"academic_year":"2021"}
When I make the request I am immediately taken to the error state and all the console log is showing below only prints "OK"
console.log(error);
The two questions are the following
Why am getting to the error when the file is successfully returning JSON
Is there a way to get a more helpful error message than just OK
You will need to set the content type to application/json
You would be better off if you used a rest API rather than using php files. .NET Core or Node.JS would give you a better development experience.
It seems that your back-end PHP send the response with status code 400. It should be revised to 200 to get the data in response. When Status code is in Error range like 400, 401, 403 ... http Response will resolved in error or catch part.
In addition if you want just get data, it's better to use GET instead of POST.

readableStream is locked while using .json()

everyone!
I got a problem: I'm trying to validate registration form. Totally, it works ok, but I need to validate form via server. In my case, for example, I need to figure out if email is already taken.
I tried to fetch and async/await syntax, but problem is still the same:
DOMException: "The operation was aborted. "
The way I understand it right now is readableStream (what actual response body is) is locked. So the wrong error is thrown, and I cannot get server response.
try {
const response = await fetch(options.url, options.requestOptions);
const body = await response.json();
if (options.modifyDataCallback instanceof Function) {
body.data = options.modifyDataCallback(body.data);
}
return body.data;
} catch (error) {
throw error;
}
How do I see the solution? I send request and recieve some server error like
code: email_in_use
message: Email '...' is already in use.
Then I need to throw error and catch it in other place in order to show corresponding error message to client.
In browsers network tab I do receive what I want to receive, but can't get the same JSON-response in my code.
Google chrome provided more information: net::ERR_HTTP2_PROTOCOL_ERROR 200.
And the problem was on backend. It is written in C# and API method returned Task. The problem was solved by adding async/await for this method.

React JS (Admin on Rest) error when API call

I'm making a API call (GET) using React js (Admin on Rest).
I already check on API server when I call to localhost:5001/cites, server return cities data, but I don't know it error on client side, here is log from browser :
failure.js:18 Error: The X-Total-Count header is missing in the HTTP Response. This header is necessary for pagination. If you are using CORS, did you declare X-Total-Count in the Access-Control-Allow-Headers header?
at convertHTTPResponseToREST (http://localhost:3000/static/js/bundle.js:33928:28)
at http://localhost:3000/static/js/bundle.js:33966:21
and
failure.js:18 Error: The Content-Range header is missing in the HTTP Response. This header is necessary for pagination. If you are using CORS, did you declare Content-Range in the Access-Control-Allow-Headers header?
at convertHTTPResponseToREST (http://localhost:3000/static/js/bundle.js:33010:28)
at http://localhost:3000/static/js/bundle.js:33037:21
May someone can help ? Thanks
Admin-on-rest needs a way to determine how many results there are in total, even if your API returns only a subset of the results - in order to build the pagination controllers. For instance, if the API returns 10 results but mentions there are 100 results in total, admin-on-rest will display 10 page links.
The simpleRestClient and jsonServerRestClient both expect to see this information in the response headers, either in X-Total-Count, or in Content-Range.
If your API doesn't include these headers, you can:
update your API to include the total as X-Total-Count header, or
write your own RESt client to grab the information about the total number of records somewhere else (e.g. in the response body)
Update your backend API function to get X-Total-Count and set it to Response Header
Example:
exports.findAll = (req, res) => {
var total = Data.countAll()// your count all function
Data.findAll({ where: condition })
.then(data => {
res.set('Access-Control-Expose-Headers', 'X-Total-Count')
res.set('X-Total-Count', total)
res.send(data);
})
.catch(err => {
res.status(500).send({
message:
err.message || "Some error occurred while retrieving data."
});
});
};

RESTful Get Request with Angular & NodeJS Express

I'm just following tutorials and figuring out how to handle get requests in NodeJS.
Here are snippets of my code:
NodeJS:
router.get('/test', function(request, response, next) {
console.log("Received Get Request");
response.jsonp({
data: 'test'
});
});
Angular:
$http.get("http://localhost:3000/test").
success(function(response) {
alert("OK");
}).
error(function(response) {
alert("FAIL");
});
If I try to access the link directly # localhost:3000/test, I'm able to receive the JSON message correctly. But when I use angularJS $http call, the request always fails and I'll find this error in the network inspector (Response)
SyntaxError:JSON.parse:unexpected end of data at line 1 column 1 of
the JSON data
The reason for that is because the response is empty but the response code is 200 in both cases.
I've tried searching for hours but maybe someone can enlighten me on this?
you could try and send
res.send('test')
and then on your http request you can use 'then'
$http.get("http://localhost:3000/test").then(function(res) {
console.log(res);
})
unlike success, then will give you a complete object (with 'test' - string as res.data)
success will bring you only the data;
then will bring you the whole object (with the status and such)..
now about that jsonp .. it's used to override a json response. you could simply use 'res.json({data: 'test'})' and it should also work for you..
hope it helps
You're using jsonp in node, which you probably don't need to. This adds extra characters to the response and so the JSON parser fails to parse (that's what the error is telling you, the JSON is malformed)
Try changing the server to look like
response.json({
data: 'test'
});
If you look in the Network pane of the developer tools, you should be able to see the raw response. It should look something like:
{"data" : "test"}

Categories