ReferenceError: product is not defined

EasyFree

New Member
#1
I'm trying to fetch the data , but when i console it.I' getting the data but in rendering it's not working in front end its showing product is not defined.
Mã:
app.get('/profile/profile/dashboard/update/:id',function (req,res,next) {
           //console.log("hello "+req.params.id);
    Product.findOne({_id: req.params.id}).then(product=>{

        console.log("hello check please "+ product);
    });

    res.render('update.ejs',{ product: product });
});
update.ejs file
Mã:
<% if (product._id) { %>
        <tbody>
        <td><input type="text" value="<%= product.name %>" required></td>
        <td><input type="text" value="" required></td>
        <td><input type="text" value="" required></td>
        <td><input type="text" value="" required></td>
        <td><input type="submit" class="btn btn-danger" value="submit"></td>
        </tbody>
        <% } %>
 

Admin

Administrator
Thành viên BQT
#2
Yes problem with your code is rendering is placed outside of the find one method. Put it inside the braces like.
Mã:
app.get('/profile/profile/dashboard/update/:id',function (req,res,next) {
           //console.log("hello "+req.params.id);
    Product.findOne({_id: req.params.id}).then(product=>{

        console.log("hello india "+ product);
        res.render('update.ejs',{ product: product });
    });
});
 

Từ khóa phổ biến

You are using an out of date browser. It may not display this or other websites correctly.
You should upgrade or use an alternative browser.

Chủ Đề Xem Nhiều

Top