我正在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); //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
调用了一个从数据库查询中返回数据的方法。
有人可以帮忙吗?
提前致谢!!