如何解决使用 node-postgres 从 Vue 组件直接连接到 postgres 数据库 - 未找到依赖项
当然,在 99.99% 的情况下,您不应直接从 SPA 连接到数据库。但我有一个特殊的用例。我的网站结合了来自多个食品数据库(如美国农业部食品数据中心)的数据,并提供了一个很好的搜索和过滤界面。我想将所有数据存储在我的数据库中,不想使用 USDA api。原因之一是 api 不是那么可靠。我下载了 USDA 数据库转储并设置了一个本地 docker postgres 数据库,我可以无限制地查询该数据库。我的站点有一个管理界面,特权用户可以在其中向我的生产数据库添加数据。首先,管理员输入美国农业部食品 ID。然后Vue组件应该连接到本地的postgres数据库并查询具体的食物。因此,每个管理员都设置了自己的本地 postgres 数据库,并将他们的连接详细信息输入到表单中。这些连接详细信息存储在浏览器的本地存储中。
我的天真方法是在 Vue 组件中使用 node-postgres,如下所示:
<template>
<v-btn @click="connect">Connect</v-btn>
</template>
<script>
import { Component,Vue } from 'nuxt-property-decorator'
import { Client } from 'pg'
@Component
export default class UsdaImport extends Vue {
async connect() {
const client = new Client({
user: 'postgres',host: 'localhost',database: 'usda_local',password: '1234',port: 5555,})
await client.connect()
const res = await client.query('Select * from food where id=1')
console.log(res.rows[0].message)
await client.end()
}
}
</script>
然而,发生了这个错误:
ERROR Failed to compile with 6 errors friendly-errors 09:06:29
These dependencies were not found: friendly-errors 09:06:29
friendly-errors 09:06:29
* dns in ./node_modules/pg/lib/connection-parameters.js
* fs in ./node_modules/pg-connection-string/index.js,./node_modules/pgpass/lib/index.js
* net in ./node_modules/pg/lib/connection.js friendly-errors 09:06:29
* pg-native in ./node_modules/pg/lib/native/client.js friendly-errors 09:06:29
* tls in ./node_modules/pg/lib/connection.js friendly-errors 09:06:29
friendly-errors 09:06:29
To install them,you can run: npm install --save dns fs net pg-native tls
我可以在我的节点后端使用 pg 包而没有依赖性问题。我的后端和前端在同一个 npm 存储库中,所以问题出在其他地方。
如果我运行 npm install --save dns fs net pg-native tls
,它会告诉我缺少更多依赖项,我应该运行 npm install --save dgram fs
。我可以随时运行它,但它一直告诉我缺少依赖项。
我想知道从 Vue 组件连接到数据库在技术上是否可行?
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。