gpt4 book ai didi

javascript - Nodejs 类型错误 : undefined is not a function

转载 作者:行者123 更新时间:2023-12-03 09:48:20 24 4
gpt4 key购买 nike

我正在关注 Treehouse Nodejs 教程并遇到了此错误。路由器中的服务器响应无法找到渲染器中的 View 方法。

router.js:10
response.view("header", {}, response);
^
TypeError: undefined is not a function

渲染器.js

var fs = require("fs");

function view(templateName, values, response) {
//Read from the template files
var fileContents = fs.readFileSync('./views/' + templateName + '.html');
//Insert values in to the content
//Write out the contents to the response
response.write(fileContents);
}

module.exports.view = view;

router.js

var Profile = require("./profile.js");
var renderer = require("./renderer.js");

//Handle HTTP route GET / and POST / i.e. Home
function home(request, response) {
//if url == "/" && GET
if(request.url === "/") {
//show search
response.writeHead(200, {'Content-Type': 'text/plain'});
response.view("header", {}, response);
response.write("Search\n");
response.end("Footer\n");
}
}

最佳答案

您没有使用renderer router.js中的任何地方.

我相信您想调用<strong>renderer</strong>.view而不是response.view .

关于javascript - Nodejs 类型错误 : undefined is not a function,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30952627/

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