错误代码:10021,错误消息:“未捕获TypeError:n(...)。connect不是函数”

问题描述 投票:-1回答:1

我正在尝试与Cloudflare工作者一起使用mongoose包。 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个回答
0
投票

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

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

© www.soinside.com 2019 - 2024. All rights reserved.