无法读取未定义的 属性 'createManager'

Cannot read property 'createManager' of undefined

我需要 sails.js 来动态创建新的数据存储适配器和连接 url,我遇到了这个解决方案 https://sailsjs.com/documentation/reference/waterline-orm/datastores/driver,其中说明我可以使用无状态驱动程序来做吧。

但不知何故我遇到了这个错误

TypeError: Cannot read property 'createManager' of undefined

这是我的示例代码:

let Driver = sails.getDatastore().driver;

let connectionUrl = `postgresql://postgres:postgres@localhost/test-db`;

let manager = (
    await Driver.createManager({ connectionString: connectionUrl })
).manager;

对了,我的sails版本是1.2.1

This property is not guaranteed to exist for all database adapters. If the datastore's underlying adapter does not support the standardized driver interface, then driver will not exist

正如文档中明确指出的,您的 数据源的底层适配器不支持标准化驱动程序接口 因此 driver 未定义,这就是您得到的原因,

TypeError: Cannot read property 'createManager' of undefined

谢谢,我可以用 npm 包来做到这一点 - machinepack-postgresql 而不是:

https://github.com/node-machine/driver-interface

let dynamicDb = require('machinepack-postgresql');

let connectionUrl = `postgresql://postgres:postgres@localhost/test-db`;

let manager = (
    await dynamicDb.createManager({ connectionString: connectionUrl })
).manager;

let db;
try {
    db = (
        await dynamicDb.getConnection({ manager: manager })
    ).connection;
} catch (err) {
    await dynamicDb.destroyManager({ manager: manager });
    return err;
}

let [err, data] = await flatry(dynamicDb.sendNativeQuery({
    connection: db,
    nativeQuery: 'select * from table1'
}));

if (err) {
    return err;
}

await dynamicDb.destroyManager({ manager: manager });