【问题标题】:TypeError: Class extends value undefined is not a constructor or null (svelte redis)TypeError: Class extends value undefined is not a constructor or null (svelte redis)
【发布时间】:2022-01-04 16:30:21
【问题描述】:

我刚开始使用 svelte,并试图制作一个以 redis 作为数据库的应用程序。我用我需要的所有 db 函数制作了一个打字稿文件,并尝试将它导入到我的苗条组件中,但是当我这样做时,我得到了以下错误

Class extends value undefined is not a constructor or null
TypeError: Class extends value undefined is not a constructor or null
    at node_modules/@node-redis/client/dist/lib/client/socket.js (http://localhost:3000/node_modules/.vite/chunk-L35TFNQI.js?v=60c87e0f:6515:46)
    at __require (http://localhost:3000/node_modules/.vite/chunk-VP3FZ6LR.js?v=60c87e0f:25:44)
    at node_modules/@node-redis/client/dist/lib/client/index.js (http://localhost:3000/node_modules/.vite/chunk-L35TFNQI.js?v=60c87e0f:9192:20)
    at __require (http://localhost:3000/node_modules/.vite/chunk-VP3FZ6LR.js?v=60c87e0f:25:44)
    at node_modules/@node-redis/client/dist/index.js (http://localhost:3000/node_modules/.vite/redis.js?v=60c87e0f:852:20)
    at __require (http://localhost:3000/node_modules/.vite/chunk-VP3FZ6LR.js?v=60c87e0f:25:44)
    at node_modules/redis/dist/index.js (http://localhost:3000/node_modules/.vite/redis.js?v=60c87e0f:2589:20)
    at __require (http://localhost:3000/node_modules/.vite/chunk-VP3FZ6LR.js?v=60c87e0f:25:44)
    at http://localhost:3000/node_modules/.vite/redis.js?v=60c87e0f:2615:21

这是我的 redis 文件(即使只有这么多,我也会遇到同样的错误)

import redis from 'redis'

export var str = "sample string"

这是我的苗条组件的脚本

<script lang="ts">
    import { str } from "../redis_test";
</script>

【问题讨论】:

  • 好的,我尝试将redis直接导入到svelte组件中,但仍然出现错误
  • @Shoejep 谢谢你的信息,我现在正在调查,但我想问你,这是从数据库中获取数据的正确方法,还是人们做其他事情,比如我们应该在后端为此创建一个 REST API 吗?
  • Can I connect directly to a Redis server from JavaScript running in a browser? 通常,你会有一个连接到数据库的服务器端。
  • @Shoejep 非常感谢,所以只是为了确保我做的一切都正确,人们通常做的是,有一个连接到数据库的服务器端,前端发出请求(REST/GraphQL ) 到适当处理这些请求的服务器(后端)
  • 是的,这听起来对我来说

标签: javascript node.js redis svelte


【解决方案1】:

通常,因为 redis - npm 包是为 Node.js 设计的,所以我会说跟随 How to use npm modules in browser? 并使用 browserify 以便它能够在浏览器中运行。

看完Can I connect directly to a Redis server from JavaScript running in a browser?,我想说你需要一个服务器端设置来连接你的redis服务器,然后你可以根据需要查询你的服务器端。

【讨论】:

    【解决方案2】:

    我遇到过类似的问题,我从阅读以上内容的理解是,这是因为浏览器客户端正在尝试读取服务器上的数据库。 我也在使用 sveltekit,我通过确保我的代码位于端点中来解决问题,该端点仅在服务器上运行,然后调用端点来获取我的数据。

    【讨论】:

      猜你喜欢
      • 2018-07-14
      • 2020-08-27
      • 2022-01-09
      • 1970-01-01
      • 2021-10-20
      • 2022-12-02
      • 2021-11-02
      • 2022-12-20
      • 2017-08-27
      相关资源
      最近更新 更多