js文件中的ejs.renderFile出错了

时间:2016-03-15 21:41:50

标签: javascript ejs

我是Node.js的新手。我在js文件中运行的以下代码出现错误:“'发生错误:renderFile(siginin.ejs)”。想不通为什么?

ejs.renderFile('./views/signin.ejs',function(err, result) {
   if (!err) {
            res.end(result);
   }
   else {
            res.end('An error occurred: renderFile(siginin.ejs)');
            console.log(err);
   }

});

错误:

cb不是函数

TypeError: cb is not a function
at Object.exports.renderFile (C:\Users\MyPC\workspace\Nodejs_Login\node_modules\ejs\lib\ejs.js:353:12)
at signin (C:\Users\MyPC\workspace\Nodejs_Login\routes\home.js:10:6)
at Layer.handle [as handle_request] (C:\Users\MyPC\workspace\Nodejs_Login\node_modules\express\lib\router\layer.js:95:5)
at next (C:\Users\MyPC\workspace\Nodejs_Login\node_modules\express\lib\router\route.js:131:13)
at Route.dispatch (C:\Users\MyPC\workspace\Nodejs_Login\node_modules\express\lib\router\route.js:112:3)
at Layer.handle [as handle_request] (C:\Users\MyPC\workspace\Nodejs_Login\node_modules\express\lib\router\layer.js:95:5)
at C:\Users\MyPC\workspace\Nodejs_Login\node_modules\express\lib\router\index.js:277:22
at Function.process_params (C:\Users\MyPC\workspace\Nodejs_Login\node_modules\express\lib\router\index.js:330:12)
at next (C:\Users\MyPC\workspace\Nodejs_Login\node_modules\express\lib\router\index.js:271:10)
at C:\Users\MyPC\workspace\Nodejs_Login\node_modules\express\lib\router\index.js:618:15

我在浏览器中访问此链接并出现以上错误: http://localhost:3000/signin

app.js:

var home = require('./routes/home'); //add label
app.get('/signin', home.signin);

这是home.js的文件:

home.js

/**
* New node file
*/
var ejs = require("ejs");
var mysql = require('./mysql');

function signin(req,res) {

ejs.renderFile('./views/signin.ejs',function(err, result) {
   // render on success
   if (!err) {
            res.end(result);
   }
   // render or error
   else {
            res.end('An error occurred: renderFile(siginin.ejs)');
            console.log(err);
   }
   });

  }


function afterSignIn(req,res)
{
// check user already exists
var getUser="select * from users where username='"+req.param("inputUsername")+"' and password='" + req.param("inputPassword") +"'";
console.log("Query is:"+getUser);

mysql.fetchData(function(err,results){
    if(err){
        throw err;
    }
    else 
    {
        if(results.length > 0){
            console.log("valid Login");
            ejs.renderFile('./views/successLogin.ejs', { data: results } , function(err, result) {
                // render on success
                if (!err) {
                    res.end(result);
                }
                // render or error
                else {
                    res.end('An error occurred: renderFile(successLogin.ejs)');
                    console.log(err);
                }
            });
        }
        else {    

            console.log("Invalid Login");
            ejs.renderFile('./views/failLogin.ejs',function(err, result) {
                // render on success
                if (!err) {
                    res.end(result);
                }
                // render or error
                else {
                    res.end('An error occurred: renderFile(failLogin.ejs).');
                    console.log(err);
                }
            });
        }
    }  
},getUser);
}

1 个答案:

答案 0 :(得分:1)

您可以在Express中将EJS设置为View Engine,并使用res.render()代替所有这些执行相同操作的额外代码。您当前的方法非常麻烦,并且没有利用Express的一些更好的部分,例如View Engine middleware

在Express

中使用它很简单
// Tell Express to render the views from ./views
app.set('views', path.join(__dirname, 'views'));

// Set EJS as the View Engine
app.set('view engine', 'ejs');

上面的代码块将EJS设置为Express的视图引擎,并将其用于res.render()函数。

然后只需在您的路线中即可

app.get('/signin', function(req, res) {
  return res.render('signin');
});

或者您可以修改home.signin()功能

signin(req, res) {
  return res.render('signin');
}