如何从mongoDB中为ReactJS中的每个组件获取不同的记录?

如何从mongoDB中为ReactJS中的每个组件获取不同的记录?,reactjs,database,mongodb,axios,mern,Reactjs,Database,Mongodb,Axios,Mern,我有16个相同的按钮,我想从我创建的mongoDB中获取数据,但我能达到的最好效果是:获取整个数据库x16次(16个按钮)或获取16次第一条记录。有没有办法获得第一条记录,然后是第二条记录,然后是第三条记录?我想稍后将它们分配给组件,如name={item.name},isOn:{item.isOn}。 下面是它的屏幕截图: 代码如下: async function getButtons(name) { try { const response = await axios.get(`/api/

我有16个相同的按钮,我想从我创建的mongoDB中获取数据,但我能达到的最好效果是:获取整个数据库x16次(16个按钮)或获取16次第一条记录。有没有办法获得第一条记录,然后是第二条记录,然后是第三条记录?我想稍后将它们分配给组件,如name={item.name},isOn:{item.isOn}。 下面是它的屏幕截图:

代码如下:

async function getButtons(name) {
try {
const response = await axios.get(`/api/buttons/${name}`);
return response,
console.log(response);
}
catch (error) {
console.log(error);
}
}

console.log(getButtons());
API:

从mongo获得文档的api服务代码在哪里?@onuriltan修复了!