【问题标题】:Heroku: MongoAPIError: database names cannot contain the character ' 'Heroku: MongoAPIError: database names cannot contain the character ' '
【发布时间】:2022-01-16 14:37:33
【问题描述】:

我正在尝试将我的 node.js 部署到 Heroku。 但它显示了这个问题:


FOODTASTE_NS=sample_restaurants 

export default class FoodsDAO{
    static async injectDB(conn) {
      if (foods) {
        return
      }
      try {
        foods = await conn.db(process.env.FOODTASTE_NS).collection("restaurants")
      } catch (e) {
        console.error(
          `Unable to establish a collection handle in foodsDAO: ${e}`,
        )
      }
    }

Heroku 抛出错误说:

Unable to establish a collection handle in foodsDAO: MongoAPIError: database names cannot contain the character ' '  

我不确定是否需要在 Mongodb 中更改数据库名称?

谢谢

【问题讨论】:

  • 数据库名称中不允许有空格。尝试记录 process.env.FOODTASTE_NS 以确保它包含什么。
  • 嗨@Joe 感谢您的建议。我尝试记录'process.env.FOODTASTE_NS'。它显示“sample_restaurants”。

标签: node.js mongodb heroku mern


【解决方案1】:

我刚刚解决了这个问题。 在 .env 文件中,其中包含所有环境变量。有一个名为 FOODTASTE_NS 的变量。

FOODTASTE_NS=sample_restaurants 

如果我不双击这个变量,我就不会知道“FOODTASTE_NS=sample_restaurants”后面有一个空格字符。正确的答案是删除该空间并将其更改为“FOODTASTE_NS=sample_restaurants”。

【讨论】:

  • 您的答案可以通过额外的支持信息得到改进。请edit 添加更多详细信息,例如引用或文档,以便其他人可以确认您的答案是正确的。你可以找到更多关于如何写好答案的信息in the help center
猜你喜欢
  • 2016-09-18
  • 2021-07-05
  • 2019-05-08
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-12-01
  • 1970-01-01
  • 2022-12-26
相关资源
最近更新 更多