为什么我不能将json数据传递给ejs模板?

时间:2017-12-06 03:13:38

标签: javascript mysql node.js ejs

我正在使用节点js和mysql

开发应用程序

我有以下功能:

function processInfo(req, res){

//connectig to the db
var db = mysql.createConnection(connection);
db.connect();

//creating the variables to insert
var tableName = 'book';
var book_code = [req.body.bookcode];
var booktitle = [req.body.booktitle];
var publishercode = req.body.publishercode;
var booktype = req.body.booktype;
var paperback = req.body.option;
var message = " ";



db.query("INSERT INTO book (bookcode, title) VALUES ('" + book_code + "' , '" + booktitle + "')", function(err, rows, fields){
    if(err) { 
        throw err; 

        if (err.code === 'ER_DUP_ENTRY') {
            message = "Error: Book code already exist. Try another book code!";
        );
        }
    //if no error continue to add 
    } else { 

    console.log(req.body);
    //res.send("data successfully added");
    res.render('maintenance/modifybooks', { message: message } );
    db.end();

    }
});
}

在我的modifybooks.ejs中,我有以下代码:

 <% if (message.lenght != 0)%>
  <div class="alert alert-dismissible alert-danger">
    <button type="button" class="close" data-dismiss="alert">&times;</button>
    <strong>Oh snap!</strong> <a href="#" class="alert-link">Change a few things up</a> and try submitting again.
  </div>
<% } %> 

但是,只要我点击提交按钮,它就会出现以下错误:

ReferenceError: /Users/arelysalvarezpadilla/Desktop/henrybooks-webapp/views/pages/modifybooks.ejs:64
62|                     <div class="form-group">
63|                         <button type="submit" class="btn btn-success btn-md" data-toggle="modal" data-target="#mymodal">Add</button>
64|                         <% if(message.length > 0) { %>
65|                             <div class="alert alert-dismissible alert-danger">
66|                               <button type="button" class="close" data-dismiss="alert">&times;</button>
67|                               <strong>Oh snap!</strong> <a href="#" class="alert-link">Change a few things up</a> and try submitting again.

message is not defined

at eval (eval at compile (/Users/arelysalvarezpadilla/Desktop/henrybooks-webapp/node_modules/ejs/lib/ejs.js:549:12), <anonymous>:12:8)
at returnedFn (/Users/arelysalvarezpadilla/Desktop/henrybooks-webapp/node_modules/ejs/lib/ejs.js:580:17)
at tryHandleCache (/Users/arelysalvarezpadilla/Desktop/henrybooks-webapp/node_modules/ejs/lib/ejs.js:223:34)
at View.exports.renderFile [as engine] (/Users/arelysalvarezpadilla/Desktop/henrybooks-webapp/node_modules/ejs/lib/ejs.js:437:10)
at View.render (/Users/arelysalvarezpadilla/Desktop/henrybooks-webapp/node_modules/express/lib/view.js:135:8)
at tryRender (/Users/arelysalvarezpadilla/Desktop/henrybooks-webapp/node_modules/express/lib/application.js:640:10)
at Function.render (/Users/arelysalvarezpadilla/Desktop/henrybooks-webapp/node_modules/express/lib/application.js:592:3)
at ServerResponse.render (/Users/arelysalvarezpadilla/Desktop/henrybooks-webapp/node_modules/express/lib/response.js:1008:7)
at ServerResponse.res.render (/Users/arelysalvarezpadilla/Desktop/henrybooks-webapp/node_modules/express-ejs-layouts/lib/express-layouts.js:77:18)
at gotoModifyBooks (/Users/arelysalvarezpadilla/Desktop/henrybooks-webapp/app/controllers/maintenance.book.controller.js:14:7)

当我console.log(message)时,它会给我留言,如果我console.log(valueof(message))它给了我字符串,但我无法将其传递给我的ejs文件。

任何想法为什么我无法从我的controller.js获取消息的价值?

0 个答案:

没有答案