gpt4 book ai didi

javascript - SailsJS 未将数据传递到 View 模板

转载 作者:行者123 更新时间:2023-11-29 18:47:47 25 4
gpt4 key购买 nike

我已经研究了几个小时,但似乎无法建立联系。

我只是想将一些数据从 Controller 传递到 View 模板。如果我在没有声明 View 模板的情况下运行路由,则浏览器将以 JSON 形式返回数据给浏览器(因此我知道 MYSQL 连接良好)。

每当我尝试连接到 MYSLQ 数据(或 Controller 中要查看的任何数据)以传递给模板时,我都会在 Sails 中收到错误消息,指出找不到数据(我知道这不是真的)因为我只要返回 JSON 数据就可以看到它)。我假设这与 SailsJS 尝试连接到 EJS 模板引擎的方式有关,或者模型、 Controller 和后端 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"
}

最佳答案

问题出在您的路线上。删除view: 'schools/index'来自

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

或者只是将其更改为

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

为什么?

在路由中,您必须指定要执行的 Controller 操作或要呈现的 View (如果是静态页面)

res.view采用可选的第一个参数作为 View 路径(相对于 views/ ),如果未发送,它将渲染 <contollerName>/<actionName>默认查看。

关于javascript - SailsJS 未将数据传递到 View 模板,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44509724/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com