我要瘋了。使用郵遞員時,api 資料正確回傳,但由于某種原因在 React 中我無法獲取資料。在進行實驗時,我注意到如果我在 api 中只傳遞一個條件,我會在 React 中得到回應,但是當傳遞兩個時我不能。請記住,這僅在反應中發生,因此郵遞員中的 API 可以完美地回傳回應。
反應片段:
const [data, setData] = useState([]);
const [loading, setLoading] = useState(true);
useEffect(() => {
const fetchData = async () => {
try {
const { data: response } = await axios.get('/api/activity/suggested');
setData(response);
} catch (error) {
console.error(error);
}
setLoading(false);
};
fetchData();
}, []);
貓鼬api:
router.get('/suggested', auth, async (req, res) => {
try {
const user = await User.findById(req.user.id);
const activity = await Activity.find({
event: 'suggest_to_user',
user: req.user.id,
})
.sort({ date: -1 })
.populate([
{
path: 'user',
},
{
path: 'ref_following',
},
{
path: 'ref_request',
},
{
path: 'ref_review',
populate: [
{
path: 'suggestion',
},
],
},
{
path: 'ref_collection',
populate: [
{
path: 'user',
},
],
},
{
path: 'ref_suggestion',
},
]);
res.json(activity);
} catch (error) {
console.error(error.message);
res.status(500).send('Server Error');
}
});
在此處洗掉條件使其在 React 中作業:
const activity = await Activity.find({
event: 'suggest_to_user',
user: req.user.id,
})
uj5u.com熱心網友回復:
我認為問題在于,當您從您的回應應用程式呼叫 API 時。它沒有找到用戶的 id。嘗試通過標頭發送授權用戶資訊。希望你覺得這很有幫助。
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/420750.html
標籤:
上一篇:將兩個mongodbfind和aggregation合二為一
下一篇:如何填充嵌入檔案的欄位?
