In the vidly app, we are asked to replace the fake movie and genre services with services to connect to a MongoDB running on our machine. In all of Mosh’s examples, he simply returns the service all like so:
export function getGenres() { return httpService.get(apiEndpoint + ‘/genres’); }
But what if you wanted to do something like add an additional genre to what is returned or maybe you wanted to return just a subset containing only some of the fields that are returned (which seems like a life-like situation).
When I try to do something like:
export async function getGenres() {
const addedGenre = { _id: “123456”, name: “Silly” };
let { data } = await httpService.get(apiEndpoint + ‘/genres’);
const genres = [addedGenre, …data];
return genres;
}
I get the following error (in the file calling getGenres() )
Unhandled Rejection (TypeError): genres1 is not iterable
The call is being made in movies.jsx
async componentDidMount() { const defaultAllGenres = { _id: "", name: "All Genres" }; const { data: genres1 } = await getGenres(); const genres = [defaultAllGenres, ...genres1];
Can this be accomplished and if so, how?