3

Here are my schemas:

1.The Records schema:

const mongoose = require('mongoose')
const RecordsSchema = new mongoose.Schema({
    Title: { type: String, required: true },
    postedby: [{
        type: mongoose.Schema.Types.ObjectId,
        ref: 'user'
    }],
    Author: { type: String, required: true },
    ISBN: { type: String, required: true },
    Review: { type: String },
    SelectedFile: { type: String },
    Likes: { type: Number, default: 0 },
    Date: { type: Date, default: Date.now() }
});
module.exports = Records = mongoose.model('record', RecordsSchema, 'record');

Here is the The user Schema:

const mongoose = require('mongoose')
const userSchema = new mongoose.Schema({
    username: { type: String },
    email: { type: String, required: true, unique: true },
    records: [{
        type: [mongoose.Schema.Types.ObjectId],
        ref: 'record'
    }],
    password: { type: String, required: true },
    Date: { type: Date, default: Date.now(), immutable: true }
});
module.exports = User = mongoose.model('user', userSchema, 'user');

The express route for getting a record:

router.get('/postedby/', (req, res) => {
    Records.findOne()
        .populate('postedby')
        .exec()
        .then(post => {
            if (!post) {
                return res.status(400).json({ msg: 'Add Posts' });
            }
            else return res.json(post);
        }).catch(err => console.error(err))
});

Result of the route:

{
    "postedby": [],
    "Likes": 0,
    "_id": "5fed8c12a4fb2c1e98ef09f6",
    "Title": "New Age",
    "Author": "Situma Prisco",
    "ISBN": "23422",
    "SelectedFile": "",
    "Review": "",
    "Date": "2020-12-31T08:30:10.321Z",
    "__v": 0
},

I'm getting a blank Array on the populated user field(posteddby) . Please help, What am I doing wrong? And yes, i do have a User Logged in

turivishal
  • 34,368
  • 7
  • 36
  • 59

1 Answers1

1

I implemented your code and Schemas, It's work for me, if you stored data correctly in the database, every things will be ok...

note :

please use find({}) instead of findOne() if data is not displayed again for postedby key, Other issues need to be addressed like mongoose version and ..., because your code and schemas is correct

it's result for me with find({}):

[
    {
        "postedby": [
            {
                "records": [
                    "5ff6bc0bd9e7437184b83f76",
                    "5ff6bc23d9e7437184b83f78",
                    "5ff6bc85f1045a4f102bc0cd",
                    "5ff6bca1f1045a4f102bc0ce",
                    "5ff6bca5f1045a4f102bc0cf",
                    "5ff6bcb3f1045a4f102bc0d0",
                    "5ff6bcc7f1045a4f102bc0d1"
                ],
                "_id": "5ff6b81df463322abc7406ec",
                "Date": "2021-01-07T07:28:15.654Z",
                "email": "a@test.com",
                "password": "$2a$12$wJVDysQxbjuRve.hYn/lbO0rskhwj6y8lwDuEWpCwHeNT/V2mybs.",
                "__v": 7
            }
        ],
        "Likes": 1,
        "Date": "2021-01-07T07:47:11.610Z",
        "_id": "5ff6bca1f1045a4f102bc0ce",
        "Title": "farsi",
        "Author": "javid1",
        "ISBN": "1",
        "Review": "1",
        "SelectedFile": "1",
        "__v": 0
    },
    {
        "postedby": [
            {
                "records": [
                    "5ff6bc0bd9e7437184b83f76",
                    "5ff6bc23d9e7437184b83f78",
                    "5ff6bc85f1045a4f102bc0cd",
                    "5ff6bca1f1045a4f102bc0ce",
                    "5ff6bca5f1045a4f102bc0cf",
                    "5ff6bcb3f1045a4f102bc0d0",
                    "5ff6bcc7f1045a4f102bc0d1"
                ],
                "_id": "5ff6b81df463322abc7406ec",
                "Date": "2021-01-07T07:28:15.654Z",
                "email": "a@test.com",
                "password": "$2a$12$wJVDysQxbjuRve.hYn/lbO0rskhwj6y8lwDuEWpCwHeNT/V2mybs.",
                "__v": 7
            }
        ],
        "Likes": 1,
        "Date": "2021-01-07T07:47:11.610Z",
        "_id": "5ff6bca5f1045a4f102bc0cf",
        "Title": "farsi",
        "Author": "javid1",
        "ISBN": "1",
        "Review": "1",
        "SelectedFile": "1",
        "__v": 0
    },
    {
        "postedby": [
            {
                "records": [
                    "5ff6bc0bd9e7437184b83f76",
                    "5ff6bc23d9e7437184b83f78",
                    "5ff6bc85f1045a4f102bc0cd",
                    "5ff6bca1f1045a4f102bc0ce",
                    "5ff6bca5f1045a4f102bc0cf",
                    "5ff6bcb3f1045a4f102bc0d0",
                    "5ff6bcc7f1045a4f102bc0d1"
                ],
                "_id": "5ff6b81df463322abc7406ec",
                "Date": "2021-01-07T07:28:15.654Z",
                "email": "a@test.com",
                "password": "$2a$12$wJVDysQxbjuRve.hYn/lbO0rskhwj6y8lwDuEWpCwHeNT/V2mybs.",
                "__v": 7
            }
        ],
        "Likes": 1,
        "Date": "2021-01-07T07:47:11.610Z",
        "_id": "5ff6bcb3f1045a4f102bc0d0",
        "Title": "riyazi",
        "Author": "javid1",
        "ISBN": "1",
        "Review": "1",
        "SelectedFile": "1",
        "__v": 0
    },
    {
        "postedby": [
            {
                "records": [
                    "5ff6bc0bd9e7437184b83f76",
                    "5ff6bc23d9e7437184b83f78",
                    "5ff6bc85f1045a4f102bc0cd",
                    "5ff6bca1f1045a4f102bc0ce",
                    "5ff6bca5f1045a4f102bc0cf",
                    "5ff6bcb3f1045a4f102bc0d0",
                    "5ff6bcc7f1045a4f102bc0d1"
                ],
                "_id": "5ff6b81df463322abc7406ec",
                "Date": "2021-01-07T07:28:15.654Z",
                "email": "a@test.com",
                "password": "$2a$12$wJVDysQxbjuRve.hYn/lbO0rskhwj6y8lwDuEWpCwHeNT/V2mybs.",
                "__v": 7
            }
        ],
        "Likes": 1,
        "Date": "2021-01-07T07:47:11.610Z",
        "_id": "5ff6bcc7f1045a4f102bc0d1",
        "Title": "zaban",
        "Author": "javid1",
        "ISBN": "1",
        "Review": "1",
        "SelectedFile": "1",
        "__v": 0
    },
    {
        "postedby": [
            {
                "records": [
                    "5ff6c275964d062f045e93d3",
                    "5ff6c283964d062f045e93d5"
                ],
                "_id": "5ff6c253964d062f045e93d2",
                "Date": "2021-01-07T08:01:21.499Z",
                "email": "b@test.com",
                "password": "$2a$12$jmHUrTSPwjaVd0VEIpsGauExHNSukHRyWWiJt4UlEgeWLBo8GPDH.",
                "__v": 2
            }
        ],
        "Likes": 1,
        "Date": "2021-01-07T08:01:21.990Z",
        "_id": "5ff6c275964d062f045e93d3",
        "Title": "zaban",
        "Author": "javid1",
        "ISBN": "1",
        "Review": "1",
        "SelectedFile": "1",
        "__v": 0
    },
    {
        "postedby": [
            {
                "records": [
                    "5ff6c275964d062f045e93d3",
                    "5ff6c283964d062f045e93d5"
                ],
                "_id": "5ff6c253964d062f045e93d2",
                "Date": "2021-01-07T08:01:21.499Z",
                "email": "b@test.com",
                "password": "$2a$12$jmHUrTSPwjaVd0VEIpsGauExHNSukHRyWWiJt4UlEgeWLBo8GPDH.",
                "__v": 2
            }
        ],
        "Likes": 1,
        "Date": "2021-01-07T08:01:21.990Z",
        "_id": "5ff6c283964d062f045e93d5",
        "Title": "tttt",
        "Author": "javid1",
        "ISBN": "1",
        "Review": "1",
        "SelectedFile": "1",
        "__v": 0
    }
]
Mohammad Yaser Ahmadi
  • 4,664
  • 3
  • 17
  • 39