【问题标题】:Convert a ColumnSet column into geometry with pg-promise使用 pg-promise 将 ColumnSet 列转换为几何
【发布时间】:2023-03-18 17:01:02
【问题描述】:

根据this,我正在使用pg-promise 创建一个ColumnSet 对象:

const cs = new pgp.helpers.ColumnSet([
    {name: 'Id',prop: 'Id'},
    {name: 'Lat',prop: 'Lat'},
    {name: 'Lng',prop: 'Lng'},
    {name: 'CreationDateTime',prop: 'CreationDateTime'},
    {name: 'Topic',prop: 'Topic'},
    {name: 'UserId',prop: 'UserId'},
    {name: 'shape',mod: ':raw',prop: 'shape',def: 'point'},
    {name: 'UserName',prop: 'UserName'},
    {name: 'appName',prop: 'appName'},
    {name: 'appVersion',prop: 'appVersion'}
], {
    table: 'Location'
});

def: 'point' point 是转换为几何的方法--这是一个值,或者我如何运行 point 方法并在此列(形状)中进行绑定?

并为批量插入编写此方法:

async function insertMany(values) {
    try {
        let results = await db.none(pgp.helpers.insert(values, cs));
    } catch (error) {
        console.log(error);
    }
}

为了转换 lat 和 lng,我写了这个方法:

const point = (lat, lng) => ({
    toPostgres: () => pgp.as.format('ST_SetSRID(ST_MakePoint($1, $2), 4326)', [Lag, Lng]),
    rawType: true
});

但是我收到了这个错误:

TypeError: Values null/undefined cannot be used as raw text

据此page

原始文本变量以 :raw 或符号 ^ 结尾,并防止文本转义。此类变量不允许为 null 或 undefined,否则方法会抛出 TypeError = Values null/undefined cannot be used as raw text。

不执行point方法时,当然那个shape字段为null。

【问题讨论】:

    标签: node.js postgresql pg-promise


    【解决方案1】:

    首先,您误用了选项prop,当目标属性名称与列名称不同时使用is documented,这不是您的情况。

    并且def 也记录在案,表示缺少属性时的值。当该属性设置为nullundefined 时,不使用def 的值。

    您正在尝试覆盖结果值,这意味着您需要使用属性init

    另一个问题 - point 实现切换案例中的变量。

    总而言之,您的代码应如下所示:

    const getPoint = col => {
        const p = col.value;
        // we assume that when not null, the property is an object of {lat, lng},
        // otherwise we will insert NULL.
        return p ? pgp.as.format('ST_SetSRID(ST_MakePoint(${lat}, ${lng}), 4326)', p) : 'NULL';
    };
    
    const cs = new pgp.helpers.ColumnSet([
        'Id',
        'Lat',
        'Lng',
        'CreationDateTime',
        'Topic',
        'UserId',
        {name: 'shape', mod: ':raw', init: getPoint},
        'UserName',
        'appName',
        'appVersion',
    ], {
        table: 'Location'
    });
    

    使用Custom Type Formatting 的版本如下所示:

    const getPoint = col => {
        const p = col.value;
        if(p) {
            return {
                toPostgres: () => pgp.as.format('ST_SetSRID(ST_MakePoint(${lat}, ${lng}), 4326)', p),
                rawType: true
               };
        }
        // otherwise, we return nothing, which will result into NULL automatically
    };
    
    const cs = new pgp.helpers.ColumnSet([
        'Id',
        'Lat',
        'Lng',
        'CreationDateTime',
        'Topic',
        'UserId',
        {name: 'shape', init: getPoint},
        'UserName',
        'appName',
        'appVersion',
    ], {
        table: 'Location'
    });
    

    【讨论】:

    • 我在第一次使用推荐时收到此错误ReferenceError: getPoint is not defined。这是我的代码codepad.org/vRWRZjIX @vitaly-t
    • @sayreskabir 这是一个 JavaScript 错误,您试图在定义之前使用 const 值。您可以看到在我的代码中getPoint 是在使用之前定义的,而不是相反。如果你想反过来,你需要把它改成function getPoint(col)
    • 谢谢楼主。 @vitaly-t
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-03-04
    • 1970-01-01
    • 2022-01-07
    • 2015-06-21
    • 2017-04-03
    相关资源
    最近更新 更多