首页 > 解决方案 > pg-promise TypeError:无效的“选项”参数

问题描述

我正在尝试将我的文件从使用pg包更改为pg-promise包。我使用的初始解决方案一切正常pg。但是,切换到pg-promise并使用文档时,我在设置过程中遇到了奇怪的错误。

最新错误:

throw new TypeError('Invalid "options" parameter: ' + JSON.stringify(options));
^
TypeError: Invalid "options" parameter: "postgres://me:complete@localhost:5432/education_be"

我的配置文件:

require('dotenv').config();
const pgp = require('pg-promise');

// === My initial setup just using 'pg'
// const { Pool } = require('pg');
// const pool = new Pool({
//  name: process.env.name,
//  password: process.env.password,
//  host: process.env.host,
//  database: 'education_be',
//  port: process.env.port
// });

// === My first attempt following the guide would receive errors like:
// === Error: Option "user" is not recognized.
const connection = {
    name: 'me',
    password: 'password',
    host: 'localhost',
    database: 'education_be',
    port: 5432
};

// === attempting (unsuccessfully) to just use the address
const db = pgp('postgres://me:complete@localhost:5432/education_be');

module.exports = db;

标签: javascriptexpresspg-promise

解决方案


我迟到了,但希望这可以帮助遇到同样问题的其他人。尽管您没有选项,但您必须在 pg-promise require 语句中包含括号。

const pgp = require('pg-promise')();

推荐阅读