首页 > 解决方案 > 带有特殊字符(特别是 & 符号)的 Heroku 配置

问题描述

我有一个 Heroku 应用程序,它需要一个带有与号 (&) 的 URI 作为配置变量。具体来说,它是一个 MongoDB Atlas 连接字符串,但这不是重点。根据Heroku Docs,我应该像这样格式化这个 CLI 命令:

heroku config:set exampleVar=www.bar.com?a=b&c=d&e=f&justkey

但是,当我尝试实际运行该命令时,我得到以下信息:

Setting exampleVar and restarting ⬢ example-heroku-app... done, v10
exampleVar: www.bar.com?a=b
'c' is not recognized as an internal or external command,
operable program or batch file.
'e' is not recognized as an internal or external command,
operable program or batch file.
'justkey' is not recognized as an internal or external command,
operable program or batch file.

看来这个&角色正在让 Heroku 将它后面的内容解释为命令?我猜我需要以某种方式逃避它,但我不知道如何。转换&\&无效,添加单引号或双引号也无效。如果有人在这里有答案,请赐教!我似乎找不到关于 Heroku Config 的特殊字符规则的像样的文档。

标签: mongodbherokuenvironment-variables

解决方案


将 & 字符用双引号括起来。下面是一个例子。

heroku config:set MONGODB_URL='mongodb+srv://hello:test@cluster0.y6red.mongodb.net/fdert?retryWrites=true"&"w=majority'

请注意单引号和双引号的使用。


推荐阅读