简体   繁体   中英

I am not getting the type of response I want

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 }); };
So anyone can please tell me or improve my code so that I can get the result what I want to achieve.

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);

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);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM