Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

deeply populating variable depth "conversation" models INSIDE of "page" models #58

Open
TheRedstoneTaco opened this issue Jun 8, 2018 · 0 comments

Comments

@TheRedstoneTaco
Copy link

TheRedstoneTaco commented Jun 8, 2018

I'm trying to deep populate (any level) some page models so I can have conversation models inside of conversation models insi... inside of a page model and actually pull all that out from the database and hand it to and ejs template to use. I am trying the npm library "mongoose-deep-populate".

When a user visits the page, the route handler pulls the page's object from the database, populates it deeply with models of type "conversation", and renders an ejs template with the end result page.

The page schema has some fields but the critical part is the conversation attribute which we are trying to deeply populate:

var pageSchema = new mongoose.Schema({
    ...
    conversation: [{
        type: mongoose.Schema.Types.ObjectId,
        ref: "Conversation"
    }]
    ...
});

The conversation schema looks approximately like this too:

var conversationSchema = new mongoose.Schema({
    ...
    conversation: [{
        type: mongoose.Schema.Types.ObjectId,
        ref: "Conversation"
    }]
    ...
});

Now we are trying to go into the page that we get from the database and deeply populate it's conversation field so we will end up with a page object that has any amount and level of nested conversation objects. I tried doing this in my routes with approximately (shortened) this code:

Page.findOne({
    title: req.params.pageTitle // is "os" in this case
}).deepPopulate("conversation").exec(function(err1, foundPage) {
    res.render(req.params.pageTitle + "/index.ejs", {
        page: foundPage
    });
});

But when I console.log the page object from the ejs file, it gives this approximate output:

{
    some attributes like id and title
    ...
    "conversation": [{
        stuff like
        "upvotes": 12,
        ...
        "conversation" : ["5b1a029994ee700731996535"],
        ...
        some other stuff like
        "created": "today",
    }]
    ...
    more attributes
}

The target output is:

{
    ...
    "conversation": [{
        ...
        "conversation": [{
            // a whole bunch of attributes and in this case the nested stuff stops here so it will just look like this
           ...
            "conversation": [],
            "id": "123",
            ...
        }, {
           ...
            "conversation": [],
            "title": "differential equations with the carpet floor",
            ...
        }]
        ...
    }]
    ...
}

I want the target output! Thanks and Jesus loves you guys!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant