Reactjs find()返回的对象的键看起来像数组的索引,而不是对象数组
我正在使用MERN制作一个restapi,React一直告诉我.map和.forEach不是我的getAll返回数据的函数。我记录了我的响应的数据类型,并得到以下结果:Reactjs find()返回的对象的键看起来像数组的索引,而不是对象数组,reactjs,mongodb,express,mongoose,fetch,Reactjs,Mongodb,Express,Mongoose,Fetch,我正在使用MERN制作一个restapi,React一直告诉我.map和.forEach不是我的getAll返回数据的函数。我记录了我的响应的数据类型,并得到以下结果: 0: {_id: "5fd2cb4e5f9960344b19fcdf", projectName: "test 1", description: "new", image: "img.png", __v: 0} 1: {_id: "5fd2
0: {_id: "5fd2cb4e5f9960344b19fcdf", projectName: "test 1", description: "new", image: "img.png", __v: 0}
1: {_id: "5fd2cb4e5f9960344b19fce0", projectName: "test 1", description: "new", image: "img.png", __v: 0}
2: {_id: "5fd2cb4e5f9960344b19fce1", projectName: "test 1", description: "new", image: "img.png", __v: 0}
3: {_id: "5fd2cb4e5f9960344b19fce2", projectName: "test
这是我的控制器:
async function crochetProject(req, res) {
try {
const crochets = await Crochet.find()
res.json(crochets);
} catch (err) {
res.json(err)
}
}
这是我的取回请求:
function fetchCrochetData(){
return fetch(BASE_URL).then(res => res.json()
)
}
我尝试了{lean:true},将数据类型强制为后端和前端的数组
我不知道在获取返回的数据库项数组时还可能犯什么错误。请在控制器中检查是否获取数组!把钩针做个日志,检查一下!并尝试发送res.json({钩针})之类的响应。请在控制器中检查您是否获得数组!把钩针做个日志,检查一下!并尝试发送类似res.json({钩针})的响应