Home > other >  How to display a field from reference model
How to display a field from reference model

Time:12-05

I want to get the name of the user, using the Reserves model, because that's the one I can display. The **User ** model is referenced in the **Reserve ** model. and the only thing I can get is the Object ID.

How can I get other field values from the User model through the reference?

Here is the code for the user model:

const mongoose = require('mongoose')

const userSchema = mongoose.Schema({
    name: {
        type: String,
        required: [true, 'Please add a name']
    },
    idnum: {
        type: String,
        required: [true, 'Please add an id number'],
        unique: true
    },
    password: {
        type: String,
        required: [true, 'Please add a password']
    },
    role: {
        type: String,
        required: [true, 'Please select a role'],
        enum: ["Faculty", "Student Officer", "Admin"]
    },
    org: {
        type: String,
        required: [true, 'Please add an organization'],
    },
    dept: {
        type: String,
        required: [true, 'Please add a deparment'],
    },
}, {
    timestamps: true
})

module.exports = mongoose.model('User', userSchema)

Here is the code for the Reserve model:

const mongoose = require('mongoose')

const reserveSchema = mongoose.Schema({
    user: {
        type: mongoose.Schema.Types.ObjectId,
        required: true,
        ref: 'Users'
    },
    purpose: {
        type: String,
        required: [true, 'Please add a purpose']
    },
    dept: {
        type: String,
        required: [true, 'Please choose a department']
    },
    org: {
        type: String,
        required: [true, 'Please choose an organization']
    },
    bldg: {
        type: String,
        required: [true, 'Please choose a building']
    },
    room: {
        type: String,
        required: [true, 'Please add a room']
    },
    date: {
        type: String,
        required: [true, 'Please add a date']
    },
    time_in: {
        type: String,
        required: [true, 'Please add a starting time']
    },
    time_out: {
        type: String,
        required: [true, 'Please add an ending time']
    },
    status: {
        type: String,
    },

}, {
    timestamps: true,
})

module.exports = mongoose.model('Reserve', reserveSchema)

Then, here is the code in fetching the from the database, using the model

function ReservesContent({reserves}) {
  
    return (
        <div class='info-container'>
            <p id='requestor'>{reserves.user}</p>
            <p id='purpose'>{reserves.purpose}</p>
            <p id='building'>{reserves.bldg}</p>
            <p id='room'>{reserves.room}</p>
            <p id='time_in'>{reserves.time_in}</p>
            <p id='time_out'>{reserves.time_out}</p>
        </div>
    );
}


export default ReservesContent
{reserves.length > 0 ? (
                  <div>
                  {reserves.map((reserve) => (
                    <ReservesContent key={reserve._id} reserves={reserve} />
                  ))}
                  </div>
                ) : (<h3>No Reservations Found</h3>)}

CodePudding user response:

For that to work you need to populate the user reference

Here's some psudo code that demonstrates how to populate a field:

Reserve.find({ /* Insert serach criteria or nothing at all */ }).populate('user');

I hope this helps! Docs: https://mongoosejs.com/docs/populate.html

CodePudding user response:

I did some research and I found a way using Schema Virtuals

I added the requestor on my reserve.model file

const reserveSchema = mongoose.Schema({
user: {
    type: mongoose.Schema.Types.ObjectId,
    required: true,
    ref: 'User'
},
requestor: {
    type: String,
    required: true
},

then I added a Schema.virtual

reserveSchema.virtual('requestor-name', {
ref: 'User',
localField: 'requestor',
foreignField: 'name',
},)

i also added that requestor field in my reserve.controller : requestor:req.user.name

  • Related