【问题标题】:I can't run the node.js server using keystone.js framework我无法使用 keystone.js 框架运行 node.js 服务器
【发布时间】:2021-10-17 16:30:42
【问题描述】:

我有使用 keystone.js 的源代码,但由于 Mongodb 连接错误,我无法运行它。 这是创建 Keystone 的代码。

const keystone = new Keystone({
  name: process.env.PROJECT_NAME,
  adapter: new Adapter({dbName}),
  mongo: 'mongodb://127.0.0.1:27017/',
  sessionStore: new MongoStore({ url: 'mongodb://localhost/' }),
  cookieSecret: 'process.env.COOKIE_SECRET',
  appVersion: {
    version: '1.0.0',
    addVersionToHttpHeaders: false,
    access: false,
  },
  cookie: {
    secure: false,
    maxAge: 1000 * 60 * 60 * 24 * 30, // 30 days
    sameSite: false
  }
});

...
await keystone.connect()

以下是错误详情:

(node:9928) UnhandledPromiseRejectionWarning: Error: No MongoDB connection URI specified.
    at resolveAllKeys (E:\Node.JS\frostbets-master-2-20210809T155720Z-001\frostbets-master-2\keystone\node_modules\@keystonejs\utils\dist\utils.cjs.dev.js:51:19)    
    at processTicksAndRejections (internal/process/task_queues.js:93:5)
    at async Keystone.connect (E:\Node.JS\frostbets-master-2-20210809T155720Z-001\frostbets-master-2\keystone\node_modules\@keystonejs\keystone\lib\Keystone\index.js:450:5)
(Use `node --trace-warnings ...` to show where the warning was created)
(node:9928) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). To terminate the node process on unhandled promise rejection, use the CLI flag `--unhandled-rejections=strict` (see https://nodejs.org/api/cli.html#cli_unhandled_rejections_mode). (rejection id: 2)
(node:9928) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.
------------------------------------------------------------------------------------------

【问题讨论】:

标签: keystonejs


【解决方案1】:

您尚未指定,但看起来您使用的是 Keystone 5,所以我将采用该假设。

您发布的代码中存在许多问题:

  • 您遇到的主要问题是您传递dbName 来初始化您的适配器,但您应该传递完整的mongoUri。 这就是您遇到的特定错误的来源。 很确定dbName 曾经是一个选项,但在当前版本的 KS5 中没有。 同样,不确定您实际使用的是哪个版本,但如果您更新了旧项目中的一些包,可能就是这样。
  • 我不确定 Keystone 配置中传递的 mongo 密钥是什么,但我认为它无效。 Mongo(用于主数据库)的任何配置都应传递给适配器。
  • 您用于创建MongoStore 实例的语法已被该软件包的当前版本弃用。 如果它对你有用,请留下它,但在下面的代码中,我使用了更新的 MongoStore.create() 语法。
  • 在您的代码中有mongodb://127.0.0.1:27017/(在mongo 键下,我认为它被忽略了)和mongodb://localhost/ 用于sessionStore。 对于大多数系统,这将引用同一个数据库,但不清楚这是否是您的故意。 在我的代码中,我将会话放在单独的数据库 (my-app-sessions) 中,但这是可选的。
  • 您的配置使用文字字符串 'process.env.COOKIE_SECRET' 作为 cookie 机密,而不是 COOKIE_SECRET 环境变量中的 。 这几乎肯定不是您想要的。

要解决这些问题,您可能需要一些类似的东西:

const { Keystone } = require('@keystonejs/keystone');
const { GraphQLApp } = require('@keystonejs/app-graphql');
const { AdminUIApp } = require('@keystonejs/app-admin-ui');
const MongoStore = require('connect-mongo');
const { MongooseAdapter: Adapter } = require('@keystonejs/adapter-mongoose');

const keystone = new Keystone({
  name: process.env.PROJECT_NAME,
  adapter: new Adapter({ mongoUri: 'mongodb://localhost/my-app' }),
  sessionStore: MongoStore.create({ mongoUrl: 'mongodb://localhost/my-app-sessions' }),
  cookieSecret: process.env.COOKIE_SECRET,
  appVersion: {
    version: '1.0.0',
    addVersionToHttpHeaders: false,
    access: false,
  },
  cookie: {
    secure: false,
    maxAge: 1000 * 60 * 60 * 24 * 30, // 30 days
    sameSite: false
  }
});

// ... 

module.exports = {
  keystone,
  apps: [new GraphQLApp(), new AdminUIApp({ name: process.env.PROJECT_NAME, enableDefaultRoute: true })],
};

在这段代码中,我将标准导出保留在底部,而不是直接调用keystone.connect(),以便我可以使用yarn keystone dev 运行它。

经过测试..

"dependencies": {
  "@keystonejs/adapter-mongoose": "^11.2.2",
  "@keystonejs/app-admin-ui": "^7.5.2",
  "@keystonejs/app-graphql": "^6.3.2",
  "@keystonejs/keystone": "^19.3.3",
  "connect-mongo": "^4.4.1"
}

【讨论】:

    猜你喜欢
    • 2015-10-27
    • 2013-10-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-05-18
    • 1970-01-01
    • 2019-10-12
    • 1970-01-01
    相关资源
    最近更新 更多