簡體   English   中英

我沒有得到我想要的響應類型

[英]I am not getting the type of response I want

當 getAllMovies API 命中時,我想得到這樣的響應。 回復:

 [{ 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." }]

但我得到這樣的回應:

 { "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." } ] }

我的 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 }); };
所以任何人都可以請告訴我或改進我的代碼,以便我可以得到我想要達到的結果。

您似乎正在發送 object 作為響應,其中鍵是fetchedMovies ,其值是同名變量的值fetchedMovies

const obj = {
    fetchedMovies
};

上面的代碼片段是用 fetchedMovies 鍵創建fetchedMovies並將其值分配為同名變量的值的簡寫。

要獲得您期望的響應,您可以直接發送變量的值作為響應。

res.status(201).json(fetchedMovies);

您將收到 object 作為響應,因為您將 object 傳遞給 .json .json()方法並在該 object 中添加fetchedMovies鍵。

要獲得所需的響應,而不是將 object 傳遞給 .json .json() ,只需傳遞fetchedMovies數組

res.status(201).json(fetchedMovies);

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM