ReferenceError:产品未定义
Posted
技术标签:
【中文标题】ReferenceError:产品未定义【英文标题】:ReferenceError : products is not defined 【发布时间】:2021-10-28 14:22:21 【问题描述】:当我尝试从 MongoDB 获取产品时,我在网上商店工作,它的返回产品未定义我使用 ejs 作为模板引擎。
<%- include('parts/header') %>
<%- include('parts/navbar') %>
<div class="container">
<div class="row">
<% for(let product of products) %>
<div class="col col-12 col-md-6 col-lg-4 col-xl-3">
<div class="card" style="width: 18rem;">
<img src="<%= product.image%> " class="card-img-top">
<div class="card-body">
<h5 class="card-title">
<a href="/product/<%= product._id%>"><%= product.name%>></a>
<p>Price: <%= product.price %> $</p>
</h5>
<form action="/cart" method="POST">
<input type="number" name="amount" class="from-control">
<input type="submit" value="Add to cart" class="btn btn-primary">
</form>
</div>
</div>
</div>
<% %>
</div>
</div>
<%- include('parts/footer') %>
app.js
const express = require('express');
const path = require('path');
const homeRouter = require('./routes/home.route');
const app = express();
app.use(express.static(path.join(__dirname, 'assets')));
app.use(express.static(path.join(__dirname, 'images')));
app.set('view engine', 'ejs');
app.set('views', 'views') //default
app.get('/', (req, res) =>
res.render('index')
)
app.use('/', homeRouter)
app.listen(3000, () =>
console.log('server listen to port 3000')
)
products.model
const mongoose = require('mongoose');
const DB_URL = 'mongodb://localhost:27017/online-shop';
const productSchema = mongoose.Schema(
name: String,
image: String,
price: Number,
description: String,
category: String
)
const Product = mongoose.model('product', productSchema)
exports.getAllProducts = () =>
//connect to db
//get products
//disconnect
return new Promise((resolve, reject) =>
mongoose.connect(DB_URL).then(() =>
return Product.find()
).then(products =>
mongoose.disconnect()
resolve(products)
).catch(err => reject(err))
)
home.controller
const productsModel = require('../models/products.model')
exports.getHome = (req, res, next) =>
//get products
//render index.ejs
productsModel.getAllProducts().then(products =>
res.render('index',
products: products
)
)
结果
参考错误:D:\programming\OnlineShop\views\index.ejs:6 4|
5| <div class="row">
6|
7| <div class="col col-12 col-md-6 col-lg-4 col-xl-3">
8| <div class="card" style="width: 18rem;">
9| <img src="<%= product.image%> " class="card-img-top">
产品未定义 在 eval (编译时的 eval (D:\programming\OnlineShop\node_modules\ejs\lib\ejs.js:662:12), :16:27) 在索引处(D:\programming\OnlineShop\node_modules\ejs\lib\ejs.js:692:17) 在 tryHandleCache (D:\programming\OnlineShop\node_modules\ejs\lib\ejs.js:272:36) 在 View.exports.renderFile [作为引擎] (D:\programming\OnlineShop\node_modules\ejs\lib\ejs.js:489:10) 在 View.render (D:\programming\OnlineShop\node_modules\express\lib\view.js:135:8) 在 tryRender (D:\programming\OnlineShop\node_modules\express\lib\application.js:640:10) 在 Function.render (D:\programming\OnlineShop\node_modules\express\lib\application.js:592:3) 在 ServerResponse.render (D:\programming\OnlineShop\node_modules\express\lib\response.js:1012:7) 在 D:\programming\OnlineShop\app.js:13:9 在 Layer.handle [as handle_request] (D:\programming\OnlineShop\node_modules\express\lib\router\layer.js:95:5) 在下一个(D:\programming\OnlineShop\node_modules\express\lib\router\route.js:137:13) 在 Route.dispatch (D:\programming\OnlineShop\node_modules\express\lib\router\route.js:112:3) 在 Layer.handle [as handle_request] (D:\programming\OnlineShop\node_modules\express\lib\router\layer.js:95:5) 在 D:\programming\OnlineShop\node_modules\express\lib\router\index.js:281:22 在 Function.process_params (D:\programming\OnlineShop\node_modules\express\lib\router\index.js:335:12) 在下一个 (D:\programming\OnlineShop\node_modules\express\lib\router\index.js:275:10)
【问题讨论】:
我会尝试在 res.renderer 检查你在产品中得到什么之前在 home.controller 上调试 【参考方案1】:我认为这是因为您的数据库为空或您的数据库无法发送数据。尝试使用
<% if(products) %>
<% %>
在 index.js 文件中的循环之前。
【讨论】:
以上是关于ReferenceError:产品未定义的主要内容,如果未能解决你的问题,请参考以下文章