我如何通过我的数据从mongoDB使用猫鼬到我的App.jsx?



目前,我有App.js连接到mongoDB使用猫鼬,我可以读/写在App.js上的数据库。但是我似乎找不到一种方法来传递数据,我使用一个函数到我的App.jsx,让它在react上渲染。我目前渲染对象从一个js文件,餐馆。js,我创建了一些数据工作时,我正在构建出的结构。我只是学习react和mongo,我已经耗尽了我目前所有的javascript函数知识,但我就是不能让它工作。请帮助

const mongoose = require('mongoose');
mongoose.connect("mongodb://127.0.0.1:27017/restaurantsDB")
const restaurantSchema = new mongoose.Schema({
name: String,
rating: Number,
image: String,
categories: [],
restaurantID: Number,
avgDeliveryTime: Number
});
const Restaurant = mongoose.model("Restaurant", restaurantSchema);
const restaurant = new Restaurant({
name: "Death Star Burgers",
rating: 4,
image: "/assets/restaurant photos/rectangle 150.png",
categories: ["burgers","fries"],
avgDeliveryTime: 30
});
Restaurant.find(function(err,all){
all.forEach(function(x){
console.log(x.name)
})
})
`import React from 'react';
import RestaurantCard from './RestaurantCard'
import Restaurant from '../restaurants'

function createRestaurantCard(restaurant){

return <RestaurantCard

key = {restaurant.id}
id = {restaurant.id}
name={restaurant.name} 
image = {restaurant.image}
ratings = {restaurant.ratings}
categories = {restaurant.categories}
averageDeliveryTime = {restaurant.averageDeliveryTime}

/>

}
function App() {
return <div className='restaurant-grid'>
{Restaurant.map(createRestaurantCard)}

</div> 

}

export default App;
`

' const restaurantData = ({id: 0,名字:"超级汉堡"图片:"/assets/restaurant photos/rectangle 150.png"分类:汉堡、鸡肉、等级:5averageDeliveryTime:"30 min"},{id: 1、名字:"死亡牛排";图片:"/assets/餐厅照片/steak.png"分类:"牛排"、"鸡肉"、收视率:4,
平均投递时间:40分钟;},{id: 2名称:"王朝中餐厅";图片:"/assets/餐厅照片/chinese.png"类别:"米"、"面"、等级:5averageDeliveryTime:"15 min"},{id: 3,姓名:"Lefmagir doner";图片:"/assets/restaurant photos/wrap.png"分类:"卷"、"什锦串"、等级:2averageDeliveryTime:"50 min"}

)导出默认的restaurantData;

我尝试使用模块。导出,但我不能返回任何东西。

我建议你在useEffect钩子中做抓取数据的部分,然后你应该有状态,然后你会setState抓取的数据,以导致你的应用程序中的渲染。

最新更新