【发布时间】: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