【问题标题】:Cannot read property 'split' of undefined error无法读取未定义错误的属性“拆分”
【发布时间】:2019-10-22 18:38:53
【问题描述】:

这是我的第一个 nodejs 邮递员程序。我正在检查 CORS 配置。当我尝试在我的邮递员中运行时完成我的代码后,我得到了错误:

TypeError: Cannot read property 'split' of undefined
            <br> &nbsp; &nbsp;at origin (C:\xampp\htdocs\nodepro\recipe\server\src\cors.js:5:34)
            <br> &nbsp; &nbsp;at C:\xampp\htdocs\nodepro\recipe\server\node_modules\cors\lib\index.js:219:13
            <br> &nbsp; &nbsp;at optionsCallback (C:\xampp\htdocs\nodepro\recipe\server\node_modules\cors\lib\index.js:199:9)
            <br> &nbsp; &nbsp;at corsMiddleware (C:\xampp\htdocs\nodepro\recipe\server\node_modules\cors\lib\index.js:204:7)
            <br> &nbsp; &nbsp;at Layer.handle [as handle_request] (C:\xampp\htdocs\nodepro\recipe\server\node_modules\express\lib\router\layer.js:95:5)
            <br> &nbsp; &nbsp;at trim_prefix (C:\xampp\htdocs\nodepro\recipe\server\node_modules\express\lib\router\index.js:317:13)
            <br> &nbsp; &nbsp;at C:\xampp\htdocs\nodepro\recipe\server\node_modules\express\lib\router\index.js:284:7
            <br> &nbsp; &nbsp;at Function.process_params (C:\xampp\htdocs\nodepro\recipe\server\node_modules\express\lib\router\index.js:335:12)
            <br> &nbsp; &nbsp;at next (C:\xampp\htdocs\nodepro\recipe\server\node_modules\express\lib\router\index.js:275:10)
            <br> &nbsp; &nbsp;at urlencodedParser (C:\xampp\htdocs\nodepro\recipe\server\node_modules\body-parser\lib\types\urlencoded.js:91:7)
            <br> &nbsp; &nbsp;at Layer.handle [as handle_request] (C:\xampp\htdocs\nodepro\recipe\server\node_modules\express\lib\router\layer.js:95:5)
            <br> &nbsp; &nbsp;at trim_prefix (C:\xampp\htdocs\nodepro\recipe\server\node_modules\express\lib\router\index.js:317:13)
            <br> &nbsp; &nbsp;at C:\xampp\htdocs\nodepro\recipe\server\node_modules\express\lib\router\index.js:284:7
            <br> &nbsp; &nbsp;at Function.process_params (C:\xampp\htdocs\nodepro\recipe\server\node_modules\express\lib\router\index.js:335:12)
            <br> &nbsp; &nbsp;at next (C:\xampp\htdocs\nodepro\recipe\server\node_modules\express\lib\router\index.js:275:10)
            <br> &nbsp; &nbsp;at expressInit (C:\xampp\htdocs\nodepro\recipe\server\node_modules\express\lib\middleware\init.js:40:5)

原产地:

function(origin, callback) {
    if(process.env.CORS_WHITELIST.split(' ').indexOf(origin) !== -1) {
        callback(null, true);
    } else {
        callback(new Error('Not Allowed By CORS Configuration'));
    }
}

我收到错误说未定义拆分。

我什至为 WHITELIST 创建了一个 .env 文件,但我仍然收到错误消息。

我在.env 文件"CORS_WHITELIST=http://localhost:3000" 中给出了下面的书面代码。

【问题讨论】:

  • 请缩进你的代码,它很难读

标签: node.js cors postman


【解决方案1】:

process.env.CORS_WHITELIST 可能未定义。使用dotenv 模块确保环境变量可在process.env 中访问。

require('dotenv').config()

默认情况下,路径将是根目录。如果 .env 文件位于不同的位置,您可以添加路径。

require('dotenv').config({ path: '/full/custom/path/to/your/env/vars' })

【讨论】:

  • 谢谢先生。有效。解决方案正如你所建议的那样,我力求给 .config()。
猜你喜欢
  • 2018-10-10
  • 1970-01-01
  • 1970-01-01
  • 2015-07-04
  • 1970-01-01
  • 2019-01-20
  • 2022-01-18
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多