SailsJS没有将数据传递给视图模板

时间:2017-06-12 22:36:50

标签: javascript mysql templates sails.js

我已经在这几个小时了,似乎无法建立连接。

我只是想将一些数据从控制器传递到视图模板。如果我在没有声明视图模板的情况下运行路由,那么浏览器会以JSON格式将数据返回给浏览器(所以我知道MYSQL连接是好的)。

每当我尝试连接到MYSLQ数据(或来自控制器的任何数据来查看)以传递给模板时,我在Sails中收到错误,指出无法找到数据(我知道这不是真的)因为我只是在返回JSON数据时可以看到它。我假设这与SailsJS尝试连接到EJS模板引擎的方式有关,或者在模型,控制器和后端的View之间没有连接的东西?

这里只是一个不起作用的简单示例,但最后我尝试连接到MYSQL数据列表。

Routes.js声明:

'GET /schools': { view: 'schools/index', controller: "SchoolsController", action:"index"},

SchoolsController.js

module.exports = {
 index: function(req, res){
    //Just return this to the view
    res.view({
        "name": "Iparra"
    })
 }
};

index.ejs模板

<div id="theschools">
 <h1>List of Schools</h1>
  <%= name %>
</div>

我从Sails.js

获得错误
{
    "stack": "ReferenceError: /Users/mnelson/Documents/Mike Files 2/Personal/MyDesign Musings/sails/test-project/views/schools/index.ejs:7\n    5|       <h1>List of Schools</h1>\n    6| \n >> 7|     <%= name %>\n    8| \n    9| </div>\n    10| \n\nname is not defined\n    at eval (eval at <anonymous> (/usr/local/lib/node_modules/sails/node_modules/ejs-locals/node_modules/ejs/lib/ejs.js:237:14), <anonymous>:30:109)\n    at eval (eval at <anonymous> (/usr/local/lib/node_modules/sails/node_modules/ejs-locals/node_modules/ejs/lib/ejs.js:237:14), <anonymous>:30:137)\n    at /usr/local/lib/node_modules/sails/node_modules/ejs-locals/node_modules/ejs/lib/ejs.js:250:15\n    at Object.exports.render (/usr/local/lib/node_modules/sails/node_modules/ejs-locals/node_modules/ejs/lib/ejs.js:288:13)\n    at Object.exports.renderFile (/usr/local/lib/node_modules/sails/node_modules/ejs-locals/node_modules/ejs/lib/ejs.js:318:20)\n    at SailsView.module.exports [as engine] (/usr/local/lib/node_modules/sails/node_modules/ejs-locals/index.js:85:7)\n    at SailsView.View.render (/usr/local/lib/node_modules/sails/node_modules/@sailshq/express/lib/view.js:76:8)\n    at Function.app.render (/usr/local/lib/node_modules/sails/node_modules/@sailshq/express/lib/application.js:561:10)\n    at ServerResponse.res.render (/usr/local/lib/node_modules/sails/node_modules/@sailshq/express/lib/response.js:845:7)\n    at ServerResponse.res.view (/usr/local/lib/node_modules/sails/lib/hooks/views/res.view.js:284:16)\n    at serveView (/usr/local/lib/node_modules/sails/lib/hooks/views/onRoute.js:61:20)\n    at routeTargetFnWrapper (/usr/local/lib/node_modules/sails/lib/router/bind.js:181:5)\n    at callbacks (/usr/local/lib/node_modules/sails/node_modules/@sailshq/express/lib/router/index.js:164:37)\n    at param (/usr/local/lib/node_modules/sails/node_modules/@sailshq/express/lib/router/index.js:138:11)\n    at pass (/usr/local/lib/node_modules/sails/node_modules/@sailshq/express/lib/router/index.js:145:5)\n    at nextRoute (/usr/local/lib/node_modules/sails/node_modules/@sailshq/express/lib/router/index.js:100:7)\n    at callbacks (/usr/local/lib/node_modules/sails/node_modules/@sailshq/express/lib/router/index.js:167:11)\n    at module.exports (/usr/local/lib/node_modules/sails/lib/hooks/cors/clear-headers.js:14:3)\n    at routeTargetFnWrapper (/usr/local/lib/node_modules/sails/lib/router/bind.js:181:5)\n    at callbacks (/usr/local/lib/node_modules/sails/node_modules/@sailshq/express/lib/router/index.js:164:37)\n    at param (/usr/local/lib/node_modules/sails/node_modules/@sailshq/express/lib/router/index.js:138:11)\n    at pass (/usr/local/lib/node_modules/sails/node_modules/@sailshq/express/lib/router/index.js:145:5)",
  "message": "/Users/mnelson/Documents/Mike Files 2/Personal/My Design Musings/sails/test-project/views/schools/index.ejs:7\n    5|       <h1>List of Schools</h1>\n    6| \n >> 7|     <%= name %>\n    8| \n    9| </div>\n    10| \n\nname is not defined",
  "path": "/Users/mnelson/Documents/Mike Files 2/Personal/My Design Musings/sails/test-project/views/schools/index.ejs"
}

2 个答案:

答案 0 :(得分:0)

问题在于您的路线。从

中删除view: 'schools/index'
'GET /schools': { view: 'schools/index', controller: "SchoolsController", action:"index"},

或者只是将其更改为

'GET /schools': { 'SchoolsController.index' },

<强>为什么吗

在路由中,您必须指定要执行的控制器操作或要渲染的视图(如果是静态页面)

res.view将可选的第一个参数作为视图路径(相对于views/),如果未发送,则默认情况下将呈现<contollerName>/<actionName>视图。

答案 1 :(得分:0)

在深入研究文档并查看其他Github示例时,我终于对其进行了整理。苏扬让我走上了正确的道路,但这并没有解决问题。它将返回数据,但它没有与视图结合。这是我更新的内容,现在它将整个数据列表拉入。我必须使用数据明确返回到视图。我不确定这是不是应该在Sails中的方式,但这就是它的工作原理。似乎路线应该处理这个问题,但事实并非如此。

控制器文件:

module.exports = {
    index: function(req, res){
      Schools.find().exec(function afterFind(err, schools) {
        if (err) return res.serverError(err);
        console.log(schools);
        return res.view('schools/index', {
          theschools: schools
        });
      });
    }
};

路线档案:

'GET /schools': {controller: 'SchoolsController', action:'index'},

另外,我需要更新config中的blueprints.js文件以删除find方法的限制。

defaultLimit: Number.MAX_VALUE

我还将模板引擎更新为pug。这是视图文件index.pug

extends ../layout

block body
  #theschools
    h1
    |  List of Schools
  p
  table.table
    tr
      th ID
      th Name
      th City
      th Country
    each school in theschools
      tr
        td= school.SID
        td= school.schoolName
        td= school.schoolCity
        td= school.schoolCountry

查看此Stackoverflow问题和答案的答案帮助我删除了控制器中的显式视图声明。 Custom view/action/controller not working in Sails JS