Node.js 使用pg promise将列集列转换为几何图形

Node.js 使用pg promise将列集列转换为几何图形,node.js,postgresql,pg-promise,Node.js,Postgresql,Pg Promise,我正在使用pg promise创建一个对象,根据: def:'point'point是转换为几何体的方法——这是一个值,或者我如何运行point方法并在该列(形状)中进行绑定 并编写此批量插入方法: async function insertMany(values) { try { let results = await db.none(pgp.helpers.insert(values, cs)); } catch (error) { conso

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

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
据此:

原始文本变量以:Raw或symbol^结尾,并防止转义文本。此类变量不允许为null或未定义,否则该方法将抛出TypeError=值null/undefined不能用作原始文本


当不执行点方法时,形状字段当然为空。

首先,您误用了选项
prop
,当目标属性名称与列名不同时,将使用该选项,而列名不是您的情况

def
,也有文档记录,表示缺少属性时的值。当属性设置为
null
undefined
时,不使用
def
的值

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

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

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

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'
});
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'
});
使用的版本如下所示:

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'
});
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'
});

我在使用first Recommendated时遇到此错误
ReferenceError:getPoint未定义。这是我的密码@vitaly-t@sayreskabir这是一个JavaScript错误,您试图在定义前使用
const
值。您可以看到,在我的代码中,
getPoint
是在使用之前定义的,而不是反过来定义的。如果您想反过来使用,则需要将其更改为
函数getPoint(col)
感谢master@维塔利-t