Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
252 views
in Technique[技术] by (71.8m points)

javascript - mongoose populate() method not working on array of objects

I am not getting the applications when I am fetching a user using the populate method on the User model. I have tried using { path: ...,model: ... } parameter as well but it doesn't work. However, when I fetch an application, the student and faculty fields are populated. What am I doing wrong?

User model:

 const userSchema = new mongoose.Schema({
    name: {
        type: String,
        required: true,
        minlength: 3,
        maxlength: 50,
    },
    email: {
        type: String,
        unique: true,
        required: true,
        minlength: 5,
        maxlength: 50,
    },
    password: {
        type: String,
        required: true,
        minlength: 5,
        maxlength: 100,
    },
    department: {
        name: {
            type: String,
            required: true,
        },
    },
    type: {
        type: String,
        required: true,
    },
    regNo: {
        type: String,
        required: true,
    },
    applications: [
        {
            type: mongoose.Schema.Types.ObjectId,
            ref: "Application",
        },
    ],
});

Application Model:

const applicationSchema = new mongoose.Schema({
    faculty: {
        type: mongoose.Schema.Types.ObjectId,
        ref: "User",
        required: true,
    },
    student: {
        type: mongoose.Schema.Types.ObjectId,
        ref: "User",
        required: true,
    },
    // TODO: use ref: "Department instead of string"
    facultyDepartment: {
        type: String,
        required: true,
    },
    studentDepartment: {
        type: String,
        required: true,
    },

    statementOfPurpose: {
        type: String,
        required: true,
    },
    status: {
        type: String,
        required: true,
    },
});

users GET route:

router.get("/:id", auth, async (req, res) => {
    try {
        const user = await User.findById(req.params.id).populate(
            "applications"
        ).select("-password");
        if (!user) {
            res.status(404).send("User not found");
            return;
        }
        res.send(user);
    } catch (error) {
        console.log("Error occurred: ", error);
    }
});

Response

{
    "department": {
        "name": "ICT"
    },
    "applications": [],
    "_id": "601d323d367a9a43d4722199",
    "email": "[email protected]",
    "name": "John",
    "regNo": "123456789",
    "type": "student",
    "__v": 0
}

EXPECTED RESPONSE

{
    "department": {
        "name": "ICT"
    },
    "applications": [

      // all applications

    ],
    "_id": "601d323d367a9a43d4722199",
    "email": "[email protected]",
    "name": "John",
    "regNo": "123456789",
    "type": "student",
    "__v": 0
}
question from:https://stackoverflow.com/questions/66063127/mongoose-populate-method-not-working-on-array-of-objects

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...