'Mongoose Population returns null always
So I'm kinda new to Relations and I'm trying to populate a Document.
Here is my Document Model:
const { Schema, model, Types } = require('mongoose');
const DmsSchema = new Schema(
{
description: {
type: String,
default: '',
},
price: {
type: String,
default: '',
},
date: {
type: String,
default: '',
},
document: {
type: Types.ObjectId,
ref: 'Upload',
default: null,
},
type: {
type: String,
default: 'rechnung',
},
},
{
timestamps: true,
}
);
module.exports = model('Dms', DmsSchema);
And here is the File Upload Schema:
const { Schema, model, Types } = require('mongoose');
const UploadSchema = new Schema(
{
name: {
type: String,
default: '',
},
stream: {
type: String,
default: '',
},
type: {
type: String,
default: '',
},
assigned: {
type: Types.ObjectId,
ref: 'Dms',
default: null,
},
},
{
timestamps: true,
}
);
module.exports = model('Upload', UploadSchema);
I have created relations between them using objects Id, and the upload does exist too, but when I fetch it it doesn't work and returns null on the filed document, I don't know why:

Here Is how i fetch the data:
fetchSingle: async (req, res) => {
try {
Dms.findById(req.params.id)
.populate('document')
.exec(function (err, data) {
if (err) {
console.log(err);
res.status(404).send({
success: false,
message: 'Something went wrong fetching the documents.',
});
}
res.status(200).send({
success: true,
data: data,
});
});
} catch (error) {
console.log(error);
res.status(500).send({
success: false,
message: 'Something went wrong fetching the documents.',
});
}
},
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
