节点v14,服务器后端需要Observable并连接到PostgreSql
- 要创建Observable,需要
import { Observable } from 'rxjs';
。 - 必须将
"type": "module"
添加到包中。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
。
我怎么能两者兼得呢?
经过多次研究和尝试,回到同一地点,简单地将require
替换为import
。作为上面的一个例子,
原始:
const pgp = require('pg-promise')();
const db = pgp({...});
新:
import pgPromise from 'pg-promise';
const pgp = pgPromise({...});
const db = pgp({...});