skip to Main Content

I have two DB

const MentorSchema = new mongoose.Schema(
    {
        firstName: {
            type: String,
            required: true,
            min: 2,
            max: 50
        },
        lastName: {
            type: String,
            required: true,
            min: 2,
            max: 50
        },
        age: {
            type: Number,
            required: true,
        },
        qualification: {
            type: String,
            required: false
        },
        yearOfExpierience: {
            type: Number,
            required: true,
        },
        pricePerLesson: {
            type: Number,
            required: false
        },
        description: {
          type: String,
          required: false,
          max: 300
        },
        email: {
            type: String,
            required: true,
            max: 50,
            unique: true
        },
        password: {
            type: String,
            required: true,
            min: 5
        },
        picturePath: {
            type: String,
            default: " "
        },
        country: {
            type: String,
            required: false
        },
        city: {
            type: String,
            required: false
        },
        availableOnline: {
            type: Boolean,
            default: true
        },
        bestMentor: {
            type: Boolean,
            default: false
        },
        groupLessons: {
            type: Boolean,
            default: true,
        }
    }, {
        toJSON: {
          virtuals: true,
        },
        toObject: {
          virtuals: true
        }
      }
);



MentorSchema.virtual('blogs', {
    ref: 'Blog',
    localField: '_id',
    foreignField: 'mentor',
    justOne: false
  });


const Mentor = mongoose.model("Mentor", MentorSchema);

export default Mentor;

and

const BlogSchema = new mongoose.Schema({
    mentor: {
        type: mongoose.Schema.ObjectId,
        ref: 'Mentor',
        required: 'true'
    },
    firstName: {
        type: mongoose.Schema.ObjectId,
        ref: 'Mentor'
    },
    text: {
        type: String,
        required: true
    }
})

const Blog = mongoose.model("Blog", BlogSchema);

export default Blog;

and controller

try {
        let query;

        query = Blog.find().populate('mentor');

        const blog = await query;
        res.status(200).json(blog)
    } catch (err) {
        res.status(500).json({error: err});
    }

When i make a get request i get a response like this

"_id": "643a7df77be2af848bb42487",
"mentor": {
"_id": "64343ab14dc32790de9a5940",
"firstName": "Ivan",
"lastName": "Petro",
}

on page i have to show firstName of mentor, how to reach it?

http://localhost:3001/blog?mentor.firstName doesn`t work

I tried to make a get request like http://localhost:3001/blog?mentor.firstName but it doesn`t work

2

Answers


  1. Use a template string:

    const data = { "_id": "643a7df77be2af848bb42487", "mentor": { "_id": "64343ab14dc32790de9a5940", "firstName": "Ivan", "lastName": "Petro" } };
    
    const endpoint = `http://localhost:3001/blog?${data.mentor.firstName}`;
    
    console.log(endpoint);
    Login or Signup to reply.
  2. If you have:

    const resp = await axios.get(`http://localhost:3001/blog`)     
        .then(function(response) {       
            // response will be an array of Blog with the mentor property populated
            response?.map(blog => console.log(blog.mentor));   
        })
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search