javascript Res.Render 不是 node.js 中的函数错误
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/34109203/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Res.Render is not a function error in node.js
提问by MBBertolucci
I am getting an "res.render is not a function error" in the Windows 10 Command Prompt when I try to run my node.js code.
当我尝试运行我的 node.js 代码时,我在 Windows 10 命令提示符中收到“res.render 不是函数错误”。
What is causing this error and how can I get rid of it?
是什么导致了这个错误,我该如何摆脱它?
Here is my .js file:
这是我的 .js 文件:
/*eslint-env node*/
//------------------------------------------------------------------------------
// node.js starter application for Bluemix
//------------------------------------------------------------------------------
// HTTP request - duas alternativas
var http = require('http');
var request = require('request');
// cfenv provides access to your Cloud Foundry environment
// for more info, see: https://www.npmjs.com/package/cfenv
var cfenv = require('cfenv');
//chama o express, que abre o servidor
var express = require('express');
// create a new express server
var app = express();
// serve the files out of ./public as our main files
app.use(express.static(__dirname + '/public'));
// get the app environment from Cloud Foundry
var appEnv = cfenv.getAppEnv();
// start server on the specified port and binding host
app.listen(appEnv.port, '0.0.0.0', function() {
// print a message when the server starts listening
console.log("server starting on " + appEnv.url);
});
app.get('/home1', function(res){
http.get('http://developers.agenciaideias.com.br/cotacoes/json', function(res){
var body = '';
res.on('data', function(chunk){
body += chunk;
});
res.on('end', function(){
var json = JSON.parse(body);
var cotacao = json["bovespa"]["cotacao"];
var CotacaoDolar= json["dolar"]["cotacao"];
var VariacaoDolar=json["dolar"]["variacao"];
var CotacaoEuro=json["euro"]["cotacao"];
var VariacaoEuro=json["euro"]["variacao"];
var Atualizacao=json["atualizacao"];
res.render('cotacao.jade',{title:'Hey', message:'Sua cota??o foi de'});
});
});
});
采纳答案by jfriend00
I don't see where jade is hooked into your Express app. In order for res.render()
to know about jade templates, you have to hook a jade handler into Express.
我没有看到 jade 是在哪里挂接到您的 Express 应用程序中的。为了res.render()
了解 jade 模板,您必须将 jade 处理程序挂接到 Express 中。
If jade is installed properly, you should be able to do this to hook it into Express:
如果 jade 安装正确,您应该可以这样做以将其挂接到 Express:
app.set('view engine', 'jade');
表达文档。
In addition, you have named two arguments in the same scope as res
. Change the name of the second one that is part of the http.get()
so you can still access the higher scoped one that is the actual Express response object.
此外,您在与res
. 更改属于 的第二个的名称,http.get()
以便您仍然可以访问作为实际 Express 响应对象的更高范围的对象。
app.get('/home1', function (res) {
http.get('http://developers.agenciaideias.com.br/cotacoes/json', function (res2) {
var body = '';
res2.on('data', function (chunk) {
body += chunk;
});
res2.on('end', function () {
var json = JSON.parse(body);
var cotacao = json["bovespa"]["cotacao"];
var CotacaoDolar = json["dolar"]["cotacao"];
var VariacaoDolar = json["dolar"]["variacao"];
var CotacaoEuro = json["euro"]["cotacao"];
var VariacaoEuro = json["euro"]["variacao"];
var Atualizacao = json["atualizacao"];
res.render('cotacao.jade', {
title: 'Hey',
message: 'Sua cota??o foi de'
});
});
});
});
回答by Quentin
You're dealing with two sets of HTTP requests/responses.
您正在处理两组 HTTP 请求/响应。
One you are receiving:
您收到的一份:
app.get('/home1', function(res){
app.get('/home1', function(res){
and one you are making:
和你正在制作的一个:
http.get('http://developers.agenciaideias.com.br/cotacoes/json', function(res){
http.get('http://developers.agenciaideias.com.br/cotacoes/json', function(res){
… but you've given them both the same variable name(res
) so one masks the other and prevents you from accessing it.
……但是您已经为它们提供了相同的变量名称( res
),因此一个会屏蔽另一个并阻止您访问它。
Change the name of one of them.
更改其中之一的名称。