gpt4 book ai didi

node.js - 当我将数据从 index.js 传递到 index.ect 时,我无法在 Node.js 上显示模型值

转载 作者:太空宇宙 更新时间:2023-11-03 23:35:12 25 4
gpt4 key购买 nike

我想在 Node.js 上显示模型值,但是浏览器返回以下错误。我想获取产品数据并显示它,但似乎index.js没有将产品数据传递给index.ect。你能告诉我如何解决这个问题吗?

错误:无法在 View 目录“/Users/toshihirokaseda/bitcore_test/Realarts/views”中查找 View “错误”

//collection.js
var express = require('express');
var router = express.Router();

var model = require('../model.js'),
User = model.User;

/* GET users listing. */
router.get('/collection', function(req, res, next) {
res.render('collection', { name: req.session.user});
});

module.exports = router;

//views/collection.ect
<!doctype html>
<% extend 'layout' %>
<div class="col-xs-12 col-sm-10 col-md-8 col-lg-8 col-sm-offset-1 col-md-offset-2 col-lg-offset-2">
<div class="status_bar">
<div class="form-group has-feedback has-feed-back-right">
<div class="row">
<div class="col-xs-4 col-xs-offset-8">
<input type="text" class="form-control" id="search" placeholder="search">
</div>
</div>
</div>
</div>
<% include 'partials/product_card.ect' %>
</div


//app.js
var express = require('express');
var path = require('path');
var favicon = require('serve-favicon');
var logger = require('morgan');
var cookieParser = require('cookie-parser');
var bodyParser = require('body-parser');
var session = require('express-session');

var routes = require('./routes/index');
var users = require('./routes/users');
var login = require('./routes/login');
var register_product = require('./routes/register_product');
var collection = require('./routes/collection');
var ECT = require('ect');

var app = express();

var MongoStore = require('connect-mongo')(session);

app.engine('ect', ECT({ watch: true, root: __dirname + '/views', ext: '.ect' }).render);
app.set('view engine', 'ect');

app.use(express.static(__dirname + '/public'));

app.use(logger('dev'));
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: false }));
app.use(cookieParser());

app.use(session({
secret: 'keyboard cat',
resave: false,
saveUninitialized: false,
store: new MongoStore({
db: 'session',
host: 'localhost',
clear_interval: 60 * 60
}),
cookie: {
httpOnly: false,
maxAge: 30 * 60 * 1000 // 30min.
}
}));

var sessionCheck = function(req, res, next) {
if (req.session.user) {
next();
} else {
res.redirect('/login');
}
};

//routing
app.use('/login', login);
app.get('/users', users);
app.get('/collection', collection);
app.get('/register_product', register_product);

app.use('/', routes);
app.use('/edit', routes);
app.use('/delete', routes);

//index.js
var express = require('express');
var router = express.Router();

var model = require('../model/product.js');
var Product = model.Product;

router.get('/', function(req, res, next){
Product.find({}, function(err, items){
res.render('index', { title: 'Menu', items:items})
});
});

//index.ect
<!doctype html>
<ul>
<% items.forEach(function(item){ %>
<li><%= item.name %>, <%= item.price %> <li>
<% }) %>
</ul>

//model/product.js
var mongoose = require('mongoose');
var db = mongoose.connect('mongodb://localhost/session');

function validator(v) {
return v.length > 0;
}

var Product = new mongoose.Schema({
name : {
type: String, validate: [validator, "Empty Error"]
},
price : {
type: Number,
default: 500
},
order_user : {
type: Array,
default: []
},
created: {
type: Date,
default: Date.now
}
});

exports.Product = db.model('Product', Product);

enter image description here

最佳答案

试试这个:

var ectRenderer = ECT({ watch: true, root: __dirname + '/views', ext : '.ect' });

app.set('views', path.join(__dirname,'/views'));
app.engine('ect',ectRenderer.render);
app.set('view engine','ect');

您应该具有目录结构:

Realarts/
├──_views
├──index.ect

此外,在您的 index.js 文件中,您也不会导出路由器。您需要添加此行,因为您的 collection.js 中已包含此行:

module.exports = router;

关于node.js - 当我将数据从 index.js 传递到 index.ect 时,我无法在 Node.js 上显示模型值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33630524/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com