Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

91
Views
How to display information from _id?

I have app.get route to display an item document that contains _id, name, description. Here's what I currently have:

app.get('/itemEdit/:itemID', isLoggedIn, (req, res) => {
    //console.log("params", req.params)
    const item_id  = req.params.itemID
    //console.log("ID", item_id)
    //Here you could query the db and find the item in the array on the user object
    User.find({ _id: req.user._id },
        (err, docs) => {
            // console.log(docs[0].userInventory)
            if (err) { console.log(`error: ${err}`) }
            else {
                console.log(docs);
                //You could render edit item page form then make a post route where you query the db and update the item
                res.render('itemEdit.ejs', {item_id, docs})
            }
        })
        console.log("Item query", item_id)
})

I know that this is grabbing the correct item as my console.log displays the correct id. Then, I try to display the data but this is where I'm hitting a wall. I have the item id but how to display the name and description that is in that same item? I tried as so:

<table>
    <!-- Table Headers -->
    <tr>
        <th>#</th>
        <th>Item Name</th>
        <th>Description</th>
    </tr>
    <!-- Table Data -->
        <tr>
            <td><%= userInventory[item_id].name %></td>
            <td><%= userInventory[item_id].description %></td>
        </tr>
        <% } %>
</table>

This gives me an error: SyntaxError: Missing catch or finally after try

Any help would be greatly appreciated!

Edit:

userSchema:

let userSchema = mongoose.Schema({
    username: String,
    firstName: String,
    lastName: String,
    email: String,
    password: String,
    userInventory: [{
            name: String,
            description: String
    }]
});
about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

From what I understand, your route '/itemEdit/:itemID' should query for that item based on the itemID being passed in params and return the item.

schemas: I suggest changing your schemas to be something like this

Item schema

{
name: String,
description: String
....
}

reference to the item schema in userInventory

User schema

{
    username: String,
    firstName: String,
    lastName: String,
    email: String,
    password: String,
    userInventory: [{
      type: mongoose.Schema.Types.ObjectId,
      ref: 'item'
    }]
}

Then your api should be something like:

app.get('/itemEdit/:itemID', isLoggedIn, (req, res) => {
    const item_id  = req.params.itemID
    Item.findById(item_Id,
        (err, doc) => {
            if (err) { console.log(`error: ${err}`) }
            else {
                res.render('itemEdit.ejs', doc)
            }
        })
        console.log("Item query", item_id)
})

itemEdit.ejs

<table>
    <!-- Table Headers -->
    <tr>
        <th>#</th>
        <th>Item Name</th>
        <th>Description</th>
    </tr>
    <!-- Table Data -->
        <tr>
            <td><%= doc.name %></td>
            <td><%= doc.description %></td>
        </tr>
        <% } %>
</table>
about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!