I am trying to use populate(), however it seems like it doesn't contain transactions in user.
Is there something wrong in my code?
The transaction Table contains userId. Therefore, I thought it would automatically contains array of transactions that matches with the userId.
User Table
Transaction Table
const mongoose = require('mongoose');
const Schema = mongoose.Schema;
const userSchema = new Schema(
{
name:
{
type: String,
required: true
},
transactions: [
{
type: Schema.Types.ObjectId,
ref: 'Transaction'
}
],
},
{
timestamps: true
}
);
module.exports = mongoose.model('User', userSchema)
const mongoose = require('mongoose');
const Schema = mongoose.Schema;
const transactionSchema = new Schema(
{
userId:
{
type: Schema.Types.ObjectId,
ref: 'User',
required: true
},
payer: String,
points:
{
type: Number,
reqruied: true
}
},
{
timestamps: true
}
)
module.exports = mongoose.model('Transaction', transactionSchema)
exports.getUsers = async (req, res, next) => {
User
.find()
//.findOne({ _id: "6009f3d8019a22479cb21a5d"})
.populate('Transaction')
.then(user => {
console.log(user)
})
}
question from:
https://stackoverflow.com/questions/65836418/mongoose-populate-does-not-contain-documents 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…