【问题标题】:Error code:10021, error message: "Uncaught TypeError: n(...).connect is not a function"错误代码:10021,错误消息:“Uncaught TypeError: n(...).connect is not a function”
【发布时间】:2019-07-28 05:25:30
【问题描述】:

我正在尝试将 mongoose 包与 Cloudflare 工作人员一起使用。 在 webpack 之后当我使用无服务器部署进行部署时,我收到此错误消息。

Serverless: Packaging service...
Serverless: Excluding development dependencies...
Serverless: deploying route: example.com/api
Serverless: ❌  Fatal Error, Script Not Deployed!
Serverless: --> Error Code:10021
--> Error Message: "Uncaught TypeError: n(...).connect is not a function
at line 950
at line 1 in n
at line 1
at line 1
"
Serverless: ✅  Routes Deployed 

在线 950

n(83).connect("mongodb://localhost/db",........

我的主要代码是

addEventListener("fetch", event => {
  event.respondWith(handleRequest(event.request));
});
const mongoose = require("mongoose");
mongoose.connect("mongodb://localhost/db", {
  useNewUrlParser: true
});

async function handleRequest(r) {
  return new Response(r);
}

我也试过 mongodb 包。 显示同样的问题。

【问题讨论】:

    标签: node.js webpack mongoose serverless cloudflare-workers


    【解决方案1】:

    这些包假定您在 Node.js 上运行并且它们使用 Node.js API。 Cloudflare Workers 不基于 Node.js。相反,它实现了 Web 平台 API 的一个子集,尤其是 Service Workers API。

    目前,您无法从 Worker 创建 TCP 连接,因此您无法直接连接到 Mongo 服务器。相反,您需要使用 fetch() API 将数据库查询表述为 HTTP 请求。

    【讨论】:

      猜你喜欢
      • 2018-07-17
      • 2023-02-17
      • 1970-01-01
      • 2017-07-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-11-17
      • 2014-09-18
      相关资源
      最近更新 更多