【发布时间】:2021-09-18 02:00:02
【问题描述】:
Node v14,服务器后端需要 Observable 并连接到 PostgreSql。
- 需要create Observable,
import { Observable } from 'rxjs';。 - 必须将
"type": "module"添加到pakage.json 中否则Warning: To load an ES module, set "type": "module" in the package.json - 但它使
require不适用于 Postgre 连接。
index.js
import { Observable } from 'rxjs';
const observable = new Observable(
subscriber =>
{
...
});
observable.subscribe({...});
console.log('just after subscribe');
要连接到PostgreSql,
const pgp = require('pg-promise')();
const db = pgp({...});
现在收到ReferenceError: require is not defined。
我怎样才能两者兼得?
【问题讨论】:
-
@vitaly-t 跟随样本
import * as pgPromise from 'pg-promise'; const pgp = pgPromise({});得到 TypeError: pgPromise is not a function pgp 版本 10.11.0 -
你需要正确配置你的 TS,正如那里解释的那样。
-
@vitaly-t 我安装了 TS
npm install typescript -g,npm install,通过cd node_modules\typescript进入 typescript 文件夹,运行tsc得到了帮助屏幕。所以我做了tsc --init,tsc --build tsconfig.json。 F5,同样的 pgPromise 不是函数。还缺少其他步骤吗? -
@vitaly-t 在没有 TS 的情况下解决。还是谢谢你!
标签: node.js postgresql pg-promise