javascript router.use TypeError:无法读取未定义的属性“使用”

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/33163594/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-28 16:11:23  来源:igfitidea点击:

router.use TypeError: Cannot read property 'use' of undefined

javascriptnode.jsexpressrouter

提问by Edward Pang

I'm new to node.js, experts, please help. I'm preparing a code to do user sync based on node.js + AWS Cognito + Facebook Login. I tried an example from this link.

我是 node.js 的新手,请专家帮忙。我正在准备基于 node.js + AWS Cognito + Facebook Login 进行用户同步的代码。我从这个链接尝试了一个例子。

Every step runs smooth until "facebook sing in with passport" section

每一步都很顺利,直到“facebook 用护照唱歌”部分

var express = require('express');
var router = express.Router();
var passport = require('passport');
...
router.use(passport.initialize());
...

After adding this part in the example, I run "npm start", the following error appeared:

在示例中添加这部分后,我运行“npm start”,出现以下错误:

C:\workspace\nodejs\CognitoExample\routes\index.js:35
router.use(passport.initialize());
      ^

TypeError: Cannot read property 'use' of undefined

What's the meaning of "Cannot read property"? How to fix the problem? Thank in advance.

“无法读取属性”是什么意思?如何解决问题?预先感谢。



After few experts help here, I can solve the problem:

经过几个专家的帮助,我可以解决问题:

  • re-install express
  • 重新安装快递

I used npm install -g express-generator@3 command yesterday, so it make my global setting to express version 3. I uninstall and install express again first.

我昨天使用了 npm install -g express-generator@3 命令,所以它使我的全局设置为 express 版本 3。我先卸载并再次安装 express。

npm uninstall -g express-generator@3
npm uninstall -g express
npm install -g express
npm install -g express-generator
  • Remove local node_modules
  • 删除本地 node_modules

After step 1, same problem is still exist, I found that i installed express 3.x in local working folder before, so I create a new working folder, restart the example code again, problem is gone

第1步后,同样的问题仍然存在,我发现我之前在本地工作文件夹中安装了express 3.x,所以我创建了一个新的工作文件夹,重新启动示例代码,问题消失了

回答by Alexandr Lazarev

The tutorial you are following is written based on 4.x version of Express framework. There were made significant changes regarding routing processin Express 4.x, comparing with 3.x version. In particular, express.Router()object was added. So, or you have to adept your code (thismight help you), in order to make it work, or to upgrade Express versions.

您接下来的教程是基于 Express 框架的 4.x 版本编写的。与 3.x 版本相比,Express 4.x 中的路由过程发生了重大变化。特别express.Router()是添加了对象。所以,或者你必须熟练你的代码(可能对你有帮助),以使其工作,或升级 Express 版本。

回答by Vidura Adikari

If you are using typescript & es6imports, and run into the same problem.

如果您使用typescript & es6导入,并遇到相同的问题。

use:

利用:

import * as passport from "passport";

instead of

代替

import passport from "passport";

refer to: Typescript handbook

参考:打字稿手册

回答by Deke

I ran into similar problem. I placed the code after app.use(express.static...)and it worked.

我遇到了类似的问题。我把代码放在后面app.use(express.static...),它起作用了。