redis 使用next.js连接到Upstash时无法读取未定义的属性(阅读'startsWith')13

ldfqzlk8  于 9个月前  发布在  Redis
关注(0)|答案(1)|浏览(108)

我正在尝试用以下代码连接到upstash的redis。但我得到一个错误。
有什么问题
[程式码]

import { Redis } from "@upstash/redis";

const redis = new Redis({
  url: process.env.UPSTASH_REDIS_URL,
  token: process.env.UPSTASH_REDIS_TOKEN
});

[错误]

- error node_modules\@upstash\redis\esm\platforms\nodejs.js (27:36) @ startsWith
- error Error [TypeError]: Cannot read properties of undefined (reading 'startsWith')      
    at new Redis (webpack-internal:///(rsc)/./node_modules/@upstash/redis/esm/platforms/nodejs.js:30:37)
    at eval (webpack-internal:///(rsc)/./app/upstash/page.tsx:10:15)
    at (rsc)/./app/upstash/page.tsx (C:\SourceCode\nextjs\.next\server\app\upstash\page.js:975:1)
    at Function.__webpack_require__ (C:\SourceCode\nextjs\.next\server\webpack-runtime.js:33:42)
    at process.processTicksAndRejections (node:internal/process/task_queues:95:5)
    at async collectGenerateParams (C:\SourceCode\nextjs\node_modules\next\dist\build\utils.js:828:17) {
  digest: undefined
}
rks48beu

rks48beu1#

很可能您的process.env.UPSTASH_REDIS_URLprocess.env.UPSTASH_REDIS_TOKENundefined(或两者都是!).你可以console.log它们,以确保它们在初始化Redis之前没有被定义。
这就是修复for this identical issue in the GitHub repo,快速扫描源代码可以发现,startWith的唯一用途是直接跟随您的URL或令牌:

if (
  configOrRequester.url.startsWith(" ") ||
)
...
if (
  configOrRequester.token.startsWith(" ") ||
)

确保阅读有关环境变量的NextJS文档,以正确使用这些变量。

相关问题