【问题标题】:Is it possible to give related items in a collection the same ID?是否可以为集合中的相关项目提供相同的 ID?
【发布时间】:2021-04-16 04:27:48
【问题描述】:

我正在构建一个简单的 MERN 应用程序,用户可以在其中提交有关咖啡馆的评论。我希望用户能够单击咖啡馆名称,然后被定向到显示该特定咖啡馆的所有评论的页面。
为此,我需要过滤评论并仅返回与特定咖啡馆有关的评论。那么如何为同一家咖啡馆的审核文件提供一个通用 ID?还是我以错误的方式接近这个?我确实设法根据咖啡馆名称调整评论,但我觉得这样做是不好的做法。

查看发布路线和模型

app.post('/api/add-review',(req,res) => {
    const review = new Review(req.body)
    review.save()
    .then(() => {
        console.log('review successfully posted')
        res.status(200)
    })
    .catch(err => {
        console.log(err)
    })
})
const mongoose = require('mongoose')
const Schema = mongoose.Schema

const reviewSchema = new Schema({
    userName:String,
    stars:String,
    title:String,
    photo:String,
    blurb:String,
    cafe:String
}, {timestamps:true})

const Review = mongoose.model('reviews', reviewSchema)

module.exports = Review

Cafe GET 路径和模型

app.get('/api/all-cafes', (req,res) => {
    Cafe.find()
    .then((result) => {
        res.send(result)
    })
    .catch(err => {
        console.log(err)
    })
})
const mongoose = require('mongoose')
const Schema = mongoose.Schema

const cafeSchema = new Schema({
    cafeName:String,
    photoURL:String,
}, {timestamps:true})

const Cafe = mongoose.model('cafes', cafeSchema)

module.exports = Cafe

这是显示咖啡馆列表的组件 - 我希望用户能够点击咖啡馆并被引导到咖啡馆的评论。

import React, {useState, useEffect} from 'react'
import axios from 'axios'
import Cafe from './Cafe'

const CafeList = () => {
    const [cafes, setCafe] = useState([])

    useEffect(() => {
        axios.get('/api/all-cafes')
        .then(cafe => {
            setCafe(cafe.data)
        })
        .catch(err => {
            console.log(err)
        })
    },[])

    return(
            <div className = 'cafe-container-container'>
                <h2>Cafes</h2>
                <Cafe cafes = {cafes}/>
            </div>

    )
}

export default CafeList
import React from 'react'
import {Link} from 'react-router-dom'

const Cafe = (props) => {
    const {cafes} = props
    return(
        <div>
            {
                cafes.map(cafe =>{
                    const {cafeName,photoURL} = cafe
                    
                    return (
                    <Link to = {`/cafe-reviews/${cafeName}`} style={{ textDecoration: 'none' }} >
                        <div className = 'cafe-container'>
                            <h2>{cafeName}</h2>
                            <img src = {photoURL}></img>
                        </div>
                    </Link>
                    )
                })
            }
        </div>
    )
}

export default Cafe

【问题讨论】:

    标签: reactjs mongodb express mongoose


    【解决方案1】:

    我认为你的方法很好。但你应该像下面这样添加 cafe 属性

    const reviewSchema = new Schema({
        userName:String,
        stars:String,
        title:String,
        photo:String,
        blurb:String,
        cafe:{
            type: mongoose.Schema.Types.ObjectId,
            required: true,
            ref: 'Cafe'
        }
    }, {timestamps:true});
    ```
    and add this in you cafeSchema
    ```
    cafeSchema.virtual('reviews', {
        ref: 'Review',
        localField: '_id',
        foreignField: 'cafe'
    });
    ```
    Using in this way has several benefits like if you are accessing any review you do not need to go back and fetch info about its cafe there will be whole cafe object not only id..there is som other things you can explore about it
    

    【讨论】:

    • 感谢您的回答 - 我尝试了您的代码,但只得到了一个空数组。
    • 哪里有空数组?我的意思是你想访问什么?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-05-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-08-29
    • 2014-08-18
    相关资源
    最近更新 更多