简体   繁体   English

表达js res.redirect未定义?

[英]express js res.redirect undefined?

Hello I am having a problem with redirecting with express js. 您好,我在使用Express JS进行重定向时遇到问题。 I have a function that triggers when a submit button is pressed. 我有一个按下提交按钮时触发的功能。 At the very end of the function, I want it to redirect to a different page. 在函数的最后,我希望它重定向到另一个页面。 When trying to use res.redirect('/results.ejs'); 尝试使用res.redirect('/results.ejs'); , I get the error TypeError: Cannot read property 'redirect' of undefined . ,出现错误TypeError: Cannot read property 'redirect' of undefined Do I have to make res global or pass it to the function somehow? 我是否必须使res全局化或以某种方式将其传递给函数?

io.on('connection', function(socket){ socket.on('query', function(qCity,qState,qLowPrice,qHighPrice){ ..... I want it to redirect here });//end of socket.on query function });//end of io.on

Thanks. 谢谢。

To answer your question, you would need res to be in the same or higher scope of your socket.on('query') . 要回答您的问题,您需要将res置于与socket.on('query')相同或更高的范围内。 So would want to "pass it to the function somehow." 因此需要“以某种方式将其传递给函数”。

Although that is not how this redirect should be handled. 虽然这不是应该如何处理此重定向。 Because you are using a web socket you are going to want to redirect the user on the client when the socket on the server responds to the client. 因为您使用的是Web套接字,所以当服务器上的套接字响应客户端时,您将要在客户端上重定向用户。 Here is a Stack Overflow question on how that is done. 这是有关如何完成的堆栈溢出问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM