Home > Enterprise >  How do I fetch User post on user timeline?
How do I fetch User post on user timeline?

Time:11-18

I have a database containing 3 collections [Users (contains registered users), Userpost (contains the post of the registered users) while Profile (contains their individual profile)]. I was able to link the Profile and Users Schemas to the Userpost Schema with their ObjectId.

What I know at present is how to fetch the Userpost and populate the User and Profile. What I want to do is to fetch all the post of a single registered user his/her timeline. What I have tried to the add the Userpost and Profile Schemas to the UserSchema by the ObjectId but each time I make a post, the Userpost on the User collection is always an empty array.

Below are my Schemas please

User Schema

const userSchema = new Schema({
    username: {
        type: String,
        required: true
    },
    roles: {
        User: {
            type: Number,
            default: 2001
        },
        Mentor: Number,
        Admin: Number
    },
    password: {
        type: String,
        required: true
    },
    userID: {
        type: String,
        required: true
    },
    refreshToken: String
});

const User = mongoose.model('user', userSchema);

module.exports = User;

Profile Schema

const ProfileSchema = new Schema({

    lastname: {
        type: String,
        required: true,
    },
    firstname: {
        type: String,
        required: true,
    },
    othernames: {
        type: String,
        required: true,
    },
    countries: {
        type: String,
        required: true,
    },
    phones: {
        type: String,
        required: true,
    },  
    User: [{
        type: Schema.Types.ObjectId,
        ref: 'user',
        required: true,
    }],   
});

const Profile = mongoose.model('profile', ProfileSchema);

module.exports = Profile;

UserPost Schema

const UserpostSchema = new Schema({
    post: {
        type: String,
        required: true
    },
    Profile: [{
        type: Schema.Types.ObjectId,
        ref: 'profile',
        required: true,
    }],
    User: [{
        type: Schema.Types.ObjectId,
        ref: 'user',
        required: true,
    }]
});

const Userpost = mongoose.model('userpost', UserpostSchema);

module.exports = Userpost;

How I populate User and Profile to the UserPost on API

router.get('/getpost/:id', (req, res) => {
    const id = req.params.id;
    Userpost.find({_id:id}).populate('User').populate('Profile').exec((err,docs) => {
        if(err) throw(err);
        res.json(docs);
    })
});

How do I fetch the entire post of a user to his timeline?

Kindly help please.

Thanks and regards

CodePudding user response:

This should work for u, In UserPost Schema you have taken array for Profile and User which isn't required, and also instead of Schema use mongoose.Schema every where in model.

User Schema

const mongoose = require('mongoose')
const userSchema = new mongoose.Schema({
   username: {
      type: String,
      required: true
   },
   roles: {
      User: {
         type: Number,
         default: 2001
      },
      Mentor: Number,
      Admin: Number
   },
   password: {
      type: String,
      required: true
   },
   userID: {
      type: String,
      required: true
   },
   refreshToken: String
});

const User = mongoose.model('user', userSchema);

module.exports = User;

Profile Schema

const mongoose = require("mongoose");
const ProfileSchema = new mongoose.Schema({
lastname: {
    type: String,
    required: true,
},
firstname: {
    type: String,
    required: true,
},
othernames: {
    type: String,
    required: true,
},
countries: {
    type: String,
    required: true,
},
phones: {
    type: String,
    required: true,
},
User: {
    type: mongoose.Schema.Types.ObjectId,
    ref: "user",
    required: true,
},
});

const Profile = mongoose.model('profile', ProfileSchema);

module.exports = Profile;

UserPost Schema

const mongoose = require("mongoose");
const UserpostSchema = new mongoose.Schema({
  post: {
    type: String,
    required: true,
  },
  Profile: {
    type: mongoose.Schema.Types.ObjectId,
    ref: "profile",
    required: true,
  },
  User: {
    type: mongoose.Schema.Types.ObjectId,
    ref: "user",
    required: true,
  },
});

const Userpost = mongoose.model("userpost", UserpostSchema);

module.exports = Userpost;
  • Related