Javascript &引用;TypeError:无法分配给只读属性";渲染Expressjs视图时
我正在node.js中构建应用程序,尝试将数据从服务器发送到视图时遇到错误:Javascript &引用;TypeError:无法分配给只读属性";渲染Expressjs视图时,javascript,node.js,express,Javascript,Node.js,Express,我正在node.js中构建应用程序,尝试将数据从服务器发送到视图时遇到错误: module.exports = function (req, res) { var cookie = parseCookie.parseCookie(req.headers.cookie); user.returnUser(cookie, function(result) { var context = result[0].first_name; console.log(context); //
module.exports = function (req, res) {
var cookie = parseCookie.parseCookie(req.headers.cookie);
user.returnUser(cookie, function(result) {
var context = result[0].first_name;
console.log(context); //console logs properly
res.render('../views/home', context); //errors out at 'context'
});
};
返回的错误为:
Unhandled rejection TypeError: Cannot assign to read only property '_locals' of Josh
at ServerResponse.render (/Users/tinzors/Documents/portal/node_modules/express/lib/response.js:952:16)
at /Users/tinzors/Documents/portal/controllers/home.js:27:7
at /Users/tinzors/Documents/portal/methods/returnUser.js:8:5
at tryCatcher (/Users/tinzors/Documents/portal/node_modules/knex/node_modules/bluebird/js/main/util.js:26:23)
at Promise._settlePromiseFromHandler (/Users/tinzors/Documents/portal/node_modules/knex/node_modules/bluebird/js/main/promise.js:507:31)
at Promise._settlePromiseAt (/Users/tinzors/Documents/portal/node_modules/knex/node_modules/bluebird/js/main/promise.js:581:18)
at Promise._settlePromises (/Users/tinzors/Documents/portal/node_modules/knex/node_modules/bluebird/js/main/promise.js:697:14)
at Async._drainQueue (/Users/tinzors/Documents/portal/node_modules/knex/node_modules/bluebird/js/main/async.js:123:16)
at Async._drainQueues (/Users/tinzors/Documents/portal/node_modules/knex/node_modules/bluebird/js/main/async.js:133:10)
at Immediate.Async.drainQueues [as _onImmediate] (/Users/tinzors/Documents/portal/node_modules/knex/node_modules/bluebird/js/main/async.js:15:14)
at processImmediate [as _immediateCallback] (timers.js:374:17)
我的看法是:
<!DOCTYPE HTML>
<html>
{{> header}}
<script type="text/javascript" src="/js/home.js"></script>
<div class="container">
<h1>Welcome home, {{first_name}}.</h1>
</div>
</html>
{{>头}
欢迎回家,{{名字}。
我不确定它是否有用,但是user.returnUser
调用了一个从DB查询返回数据的方法
有人能帮忙吗
提前谢谢 我刚刚更新了我的问题w/我的视图代码@user2864740Ah。。我怀疑可以通过
context={first\u name:result[0].first\u name}
甚至context=result[0]
获得所需的结果。使用字符串作为模板上下文可能是错误的,尽管如果是这种情况,则需要深入研究实现。另外,检查结果是否如预期的那样。这很有效@user2864740我刚刚更新了我的问题,我的视图代码为@user2864740Ah。。我怀疑可以通过context={first\u name:result[0].first\u name}
甚至context=result[0]
获得所需的结果。使用字符串作为模板上下文可能是错误的,尽管如果是这种情况,则需要深入研究实现。另外,检查结果是否如预期的那样。这很有效@用户2864740