从终端获取对 PocketBase 的请求失败,但在 Postman 和浏览器中有效 |下一个.js13

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

我正在使用

create-next-app@latest
PocketBase 作为数据库构建 Next.js 应用程序。

我的问题是我的本地 PocketBase 实例拒绝来自 NextJS 应用程序的连接,但当我使用 Postmanbrowser 获取数据时检索数据没有问题。这个问题似乎与

undici
库有关,尽管我不确定它是否是问题的实际根源。

当我尝试连接到localhost时,fetch请求似乎失败,但任何外部链接(例如

google.com
)都可以正常工作

这是导致错误的代码片段

async function getNotes(){
    const res = await fetch('http://localhost:8090/api/collections/notes/records');
    const data = res.json();
    return data;
}
import PocketBase from 'pocketbase';

async function getNotes(){
        const pb = new PocketBase('http://127.0.0.1:8090')
        const list = await pb.collection('notes').getList(1, 100, {});
        return list;
}

使用内置 fetch API 时的错误日志

TypeError: fetch failed
    at Object.fetch (node:internal/deps/undici/undici:11457:11)
    at process.processTicksAndRejections (node:internal/process/task_queues:95:5) {
  cause: Error: connect ECONNREFUSED 127.0.0.1:8090
      at TCPConnectWrap.afterConnect [as oncomplete] (node:net:1494:16)
      at TCPConnectWrap.callbackTrampoline (node:internal/async_hooks:130:17) {
    errno: -111,
    code: 'ECONNREFUSED',
    syscall: 'connect',
    address: '127.0.0.1',
    port: 8090
  }
}

使用PocketBase ORM时的错误日志

ClientResponseError 0: Something went wrong while processing your request.
    at new ClientResponseError (webpack-internal:///(sc_server)/./node_modules/pocketbase/dist/pocketbase.es.mjs:161:23)
    at eval (webpack-internal:///(sc_server)/./node_modules/pocketbase/dist/pocketbase.es.mjs:1619:39)
    at process.processTicksAndRejections (node:internal/process/task_queues:95:5)
    at async getNotes (webpack-internal:///(sc_server)/./app/notes/page.tsx:21:22)
    at async NotesPage (webpack-internal:///(sc_server)/./app/notes/page.tsx:31:19) {
  url: '',
  status: 0,
  response: {},
  isAbort: false,
  originalError: TypeError: fetch failed
      at Object.fetch (node:internal/deps/undici/undici:11576:11)
      at process.processTicksAndRejections (node:internal/process/task_queues:95:5) {
    cause: Error: connect ECONNREFUSED 127.0.0.1:8090
        at TCPConnectWrap.afterConnect [as oncomplete] (node:net:1574:16)
        at TCPConnectWrap.callbackTrampoline (node:internal/async_hooks:130:17) {
      errno: -111,
      code: 'ECONNREFUSED',
      syscall: 'connect',
      address: '127.0.0.1',
      port: 8090
    }
  }
}

使用“node-fetch”npm库时的错误日志(这次错误日志不包括Undici)

FetchError: request to http://127.0.0.1:8090/api/collections/notes/records?page=1&perPage=30 failed, reason: connect ECONNREFUSED 127.0.0.1:8090
    at ClientRequest.eval (webpack-internal:///(sc_server)/./node_modules/node-fetch/src/index.js:124:20)      
    at ClientRequest.emit (node:events:511:28)
    at Socket.socketErrorListener (node:_http_client:495:9)
    at Socket.emit (node:events:511:28)
    at emitErrorNT (node:internal/streams/destroy:151:8)
    at emitErrorCloseNT (node:internal/streams/destroy:116:3)
    at process.processTicksAndRejections (node:internal/process/task_queues:82:21) {
  type: 'system',
  errno: 'ECONNREFUSED',
  code: 'ECONNREFUSED',
  erroredSysCall: 'connect'
}

我已经尝试了几乎所有我能找到的东西,例如:
1.更改节点版本并更新本地和全局npm包
2. 使用 'node-fetch' 和 'axios' 等第三方库连接数据库。 3. 使用 PocketBase npm 包 (ORM) 连接到数据库,并在运行 PocketBase 后仅使用 fetch API。请注意,管理面板工作正常。
4. 在

http://localhost:....
http://127.0.0.1:...
url 之间进行更改会产生完全相同的结果。

node.js next.js localhost pocketbase undici
1个回答
0
投票

我无法确定问题的根源,

但是,目前看来切换到非基于 chromium 的浏览器是一个有效的解决方法

这是 github 问题,其中包含有关该问题的更多信息

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