Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

92
Visualizações
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 Respostas
Responde à pergunta

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda