Node.js findById通过其Id显示产品详细信息时出现问题
我是nodeJs的新手,想练习创建购物车。我一个人都成功了,但我设法显示了用户单击的产品的详细信息,但它不起作用: 我的代码摘要 我在models文件夹中创建了两个名为user.js和jacker.js的js文件,每个文件分别包含一个模式和模型。在我的控制器文件夹中,我有一个index.js文件,其中包括:Node.js findById通过其Id显示产品详细信息时出现问题,node.js,mongodb,express,Node.js,Mongodb,Express,我是nodeJs的新手,想练习创建购物车。我一个人都成功了,但我设法显示了用户单击的产品的详细信息,但它不起作用: 我的代码摘要 我在models文件夹中创建了两个名为user.js和jacker.js的js文件,每个文件分别包含一个模式和模型。在我的控制器文件夹中,我有一个index.js文件,其中包括: const User = require ("../models/users") const Jacket = require ('../models/jacket')
const User = require ("../models/users")
const Jacket = require ('../models/jacket')
我的问题是,我想同时访问这两个文档的Id:这是我的代码
const getProductId = (req, res, next) => {
const id = req.params.id;
const Users = User;
const Jackets = Jacket;
!!!. findById(id, (error, product) => {
if (error) console.log(error);
console.log(product);
res.render("product-detail", {
product: product
});
});
};
module.exports = { getProductId: getProductId };
“我的路线”文件夹:
const express = require ('express')
const router = express.Router ()
const getIndexController = require ('../controllers/index')
router.get ('/product/:id', getIndexController.getProductId)
由于默认情况下mongodb会自动生成id,因此在两个不同的集合中使用相同的id似乎不太可能,除非您自定义id 但无论如何。。。这是一个基于承诺的解决方案
Promise.all([
User.findById(id).exec(),
Jacket.findById(id).exec()
])
.then(([user, jacket]) => {
// Or however your template might look like
res.render('product-detail', { user, jacket } )
})
.catch(error =>{
console.log(error)
})
这里的关键是mongo不会在向数据库发送查询后等待,除非您强制它这样做
如果您坚持使用回调,则必须将其链接:
User.findById (id, (error, user) => {
Jacket.findById (id, (error, jacket) => {
// This is what we call "callback hell"
// It might not seem so bad, but it quickly builds up.
默认情况下,mongodb自动生成id,因此在两个不同的集合中使用相同的id似乎不太可能,除非您自定义id 但无论如何。。。这是一个基于承诺的解决方案
Promise.all([
User.findById(id).exec(),
Jacket.findById(id).exec()
])
.then(([user, jacket]) => {
// Or however your template might look like
res.render('product-detail', { user, jacket } )
})
.catch(error =>{
console.log(error)
})
这里的关键是mongo不会在向数据库发送查询后等待,除非您强制它这样做
如果您坚持使用回调,则必须将其链接:
User.findById (id, (error, user) => {
Jacket.findById (id, (error, jacket) => {
// This is what we call "callback hell"
// It might not seem so bad, but it quickly builds up.
我试过了
const getProductId = (req, res, next) => {
const id = req.params.id Promise.all ([ User.findById (id) .exec (), Jacket.findById
(id) .exec ()])
.then (([product]) => { res.render ('product-detail', {product}) })
.catch (error => { console.log (error) }) }
它只对用户有效
这是我的ejs文件中的代码
<h1> <% = product.name%> </h1>
<img width = "200px" height = "200px" src = "<% = product.image%>" alt = "<% =
product.image%>" />
<h1> <% = product.description%> </h1> <h1> <% = product.prix%> </h1>
“alt=”“/>
我试过了
const getProductId = (req, res, next) => {
const id = req.params.id Promise.all ([ User.findById (id) .exec (), Jacket.findById
(id) .exec ()])
.then (([product]) => { res.render ('product-detail', {product}) })
.catch (error => { console.log (error) }) }
它只对用户有效
这是我的ejs文件中的代码
<h1> <% = product.name%> </h1>
<img width = "200px" height = "200px" src = "<% = product.image%>" alt = "<% =
product.image%>" />
<h1> <% = product.description%> </h1> <h1> <% = product.prix%> </h1>
“alt=”“/>
而不是od“id”try“\u id”猫鼬在这里没有提到,但是我们可以假设用户和夹克是猫鼬模型吗?@HimanshuBhardwajfindById(id)
等于findByOne({u id:id})
所以在这里使用\u id
是错误的。是的,Veste和用户是猫鼬模型而不是od“id”try“\u id”这里没有提到猫鼬,但是我们可以假设用户和夹克是猫鼬模型吗?@HimanshuBhardwajfindById(id)
等于findByOne({u id:id})
,所以在这里使用\u id
是错误的。是的,维斯特和用户是猫鼬模型