Reputation: 868
I want to get a response like this when getAllMovies API hit. Response:
[{
name: "Harry Potter and the Order of the Phoenix",
img: "bla",
summary: "Harry Potter and Dumbledore's warning about the return of Lord Voldemort is not heeded by the wizard authorities who, in turn, look to undermine Dumbledore's authority at Hogwarts and discredit Harry."
}, {
name: "The Lord of the Rings: The Fellowship of the Ring",
img: "bla",
summary: "A young hobbit, Frodo, who has found the One Ring that belongs to the Dark Lord Sauron, begins his journey with eight companions to Mount Doom, the only place where it can be destroyed."
}, {
name: "Avengers: Endgame",
img: "bla",
summary: "Adrift in space with no food or water, Tony Stark sends a message to Pepper Potts as his oxygen supply starts to dwindle. Meanwhile, the remaining Avengers -- Thor, Black Widow, Captain America and Bruce Banner -- must figure out a way to bring back their vanquished allies for an epic showdown with Thanos -- the evil demigod who decimated the planet and the universe."
}]
But I am getting a response like this:
{
"fetchedMovies": [
{
"name": "Harry Potter and the Order of the Phoenix",
"img": "bla",
"summary": "Harry Potter and Dumbledore's warning about the return of Lord Voldemort is not heeded by the wizard authorities who, in turn, look to undermine Dumbledore's authority at Hogwarts and discredit Harry."
},
{
"name": "The Lord of the Rings: The Fellowship of the Ring",
"img": "bla",
"summary": "A young hobbit, Frodo, who has found the One Ring that belongs to the Dark Lord Sauron, begins his journey with eight companions to Mount Doom, the only place where it can be destroyed."
},
{
"name": "Avengers: Endgame",
"img": "bla",
"summary": "Adrift in space with no food or water, Tony Stark sends a message to Pepper Potts as his oxygen supply starts to dwindle. Meanwhile, the remaining Avengers -- Thor, Black Widow, Captain America and Bruce Banner -- must figure out a way to bring back their vanquished allies for an epic showdown with Thanos -- the evil demigod who decimated the planet and the universe."
}
]
}
My code for getAllMovies:
const getAllMovies = async (req, res) => {
let fetchedMovies;
try {
fetchedMovies = await Movie.find({},'name img summary -_id');
} catch (err) {
console.log(err)
const error = new HttpResponse(
err,
500
);
return res.status(500).json({ response: error })
}
res.status(201).json({
fetchedMovies
});
};
Upvotes: 1
Views: 52
Reputation: 29282
You are getting an object as a response because you are passing an object to .json()
method and adding fetchedMovies
key in that object.
To get the desired response, instead of passing an object to .json()
, just pass the fetchedMovies
array
res.status(201).json(fetchedMovies);
Upvotes: 1
Reputation: 1790
You seem to be sending an object as response where key is fetchedMovies
and its value it value of the variable by same name fetchedMovies
.
const obj = {
fetchedMovies
};
the above snippet is short hand for creating object with key fetchedMovies
and assigning its value as the value of same named variable.
To get the response you are expecting, you can just send the value of the variable directly in response.
res.status(201).json(fetchedMovies);
Upvotes: 1