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

时间:2022-03-10 21:22:17

I'd like to show model values on Node.js, however the browser returns the following err. I want to get the product data and show it, but it seems that index.js does not pass product data to index.ect. Could you tell how to solve the problem?

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

Error: Failed to lookup view "error" in views directory "/Users/toshihirokaseda/bitcore_test/Realarts/views"

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

//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);

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

1 个解决方案

#1


2  

Try This:

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');

You should have directory structure:

你应该有目录结构:

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

Also in your index.js file, you are not exporting the router. You need to add this line as you have it in your collection.js:

同样在index.js文件中,您没有导出路由器。您需要在collection.js中添加此行:

module.exports = router;

#1


2  

Try This:

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');

You should have directory structure:

你应该有目录结构:

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

Also in your index.js file, you are not exporting the router. You need to add this line as you have it in your collection.js:

同样在index.js文件中,您没有导出路由器。您需要在collection.js中添加此行:

module.exports = router;