消息未定义 - error.ejs

时间:2016-12-08 20:27:52

标签: javascript node.js express

如果我搜索http://localhost:3000/buyersearch/buyersearch,我会收到此错误:

enter image description here

我的所有其他路线除此之外都有效。

该路由从error.ejs收到错误。为什么呢?

buyerSearch.js

var express = require('express');
var router = express.Router();
var mongodb = require('mongodb');
var MongoClient = mongodb.MongoClient;
var url = 'mongodb://localhost:27017/WishList';

//buyerIDSearch
router.get('/buyerSearch', function (req, res) {
  var id = req.query.buyerID;

  if (!id || !parseInt(id)) {
    res.render('error', {
      error: "Please enter an Buyer Identification Number"
    });
  } else {
    mongoClient.connect(url, function (err, db) {
      if (err) {
        res.render('error', {
          error: "Failed to connect"
        });
      } else {
        var WishListDB = db.collection('orders');
        WishListDB.find({
          "_buyerID": parseInt(id)
        }).toArray(function (err, result) {
          if (err || !result || result.length == 0) {
            res.render('error', {
              error: "No order found with that ID number"
            });
          } else {
            res.render('order', {
              order: result[0]
            })
          }
        });
      }
    })
  }
});

module.exports = router;

app.js参考

var buyerSearch = require('./routes/buyerSearch');
app.use('/buyerSearch', buyerSearch);

**buyerSearch.ejs**

<!DOCTYPE html>
<html>
<head>
  <title></title>
  <link rel='stylesheet' href='/stylesheets/style.css' />
</head>
<body>
  <h1></h1>
  <p>Welcome</p>

  <li><%buyerSearch.buyerID %></li>
</body>
</html>

2 个答案:

答案 0 :(得分:2)

您的error.html模板似乎需要像这样调用:

res.render("error", {
   message: "Please enter an Buyer Identification Number",
   error: {status:"", stack:""}
});

必须调整状态和堆栈值。它们可能来自Error对象。

答案 1 :(得分:0)

res.render(&#34;错误&#34;,{    消息:&#34;请输入买方识别号码&#34;,    错误:{状态:&#34;&#34;,堆栈:&#34;&#34;} });