【问题标题】:How to add variable into JSON path如何将变量添加到 JSON 路径中
【发布时间】:2021-08-18 08:36:45
【问题描述】:

这是我正在使用的查询:

app.get("/items/:data", async (req, res) => {

    const { data } = req.params;

    query = `
        SELECT items.discount
        FROM items
        WHERE items.discount @? '$[*] ? (@.discount[*].shift == $1)'
        `
    try {

        const obj = await pool.query(query, [data]);
        res.json(obj.rows[0])

    } catch(err) {

        console.error(err.message);

    }
});

我收到此错误:

error: bind message supplies 1 parameters, but prepared statement "" requires 0

我在node.js 中使用node-postgres 包。

我该如何解决这个问题?

【问题讨论】:

    标签: node-postgres node-pg-pool


    【解决方案1】:

    使用括号表示法而不是点表示法。所以代替 obj.key 使用 obj[key]

    【讨论】:

    • 你能用代码回答吗?我需要在查询中使用 $1 而不是“部分”。
    【解决方案2】:

    更新

    所有这些驱动程序连接器都有自己的方法来完成您正在寻找的工作。 node-postgres 也有自己的

    
    import { Pool } from 'pg';
    
    const pool = new Pool({
      host: 'localhost',
      user: 'database-user',
      max: 20,
      idleTimeoutMillis: 30000,
      connectionTimeoutMillis: 2000,
    });
    
    /**
     * execs the given sql statement.
     * 
     * @param {string} sql - query to run.
     * @param {Array} params - an array with the parameter.
     * @example
     * runQuery("SELECT * FROM users WHERE id = $1", [1]).then(result=> console.log(result))
     */
    export async function runQuery (sql, params) {
    const connection = await pool.connect()
      try {
        await connection.query('BEGIN')
        const queryText = 'INSERT INTO users(name) VALUES($1) RETURNING id'
        const result = await connection.query(sql,params);
        
        // check what result has
        console.log(result);
    
        return connection.query('COMMIT').then(result)
      } catch (e) {
        await connection.query('ROLLBACK')
        throw e;
        throw e
      } finally {
        connection.release()
      }
    }
    
    
    

    池配置

    config = {
      // all valid client config options are also valid here
      // in addition here are the pool specific configuration parameters:
      // number of milliseconds to wait before timing out when connecting a new client
      // by default this is 0 which means no timeout
      connectionTimeoutMillis?: int,
      // number of milliseconds a client must sit idle in the pool and not be checked out
      // before it is disconnected from the backend and discarded
      // default is 10000 (10 seconds) - set to 0 to disable auto-disconnection of idle clients
      idleTimeoutMillis?: int,
      // maximum number of clients the pool should contain
      // by default this is set to 10.
      max?: int,
    }
    
    

    结论

    所以基本上查询的结构应该是这样或更少

    const text = 'INSERT INTO users(name, email) VALUES($1, $2) RETURNING *'
    const values = ['brianc', 'brian.m.carlson@gmail.com']
    
    connection
      .query(text, values)
      .then(res => {
        console.log(res.rows[0])
        // { name: 'brianc', email: 'brian.m.carlson@gmail.com' }
      })
      .catch(e => console.error(e.stack))
    
    

    【讨论】:

    • 我没明白这一点。那么如何在我的查询中将“部分”替换为 $1 并使其仍然有效?
    • 我认为这个解决方案存在 SQL 注入漏洞。因为客户端发送的数据会被替换成SQL而不做检查。如果我错了,请纠正我
    • 标题 如何将变量添加到 JSON 路径中 并没有告诉我有关 sql 的任何信息,请检查 sn-p 我更新它
    猜你喜欢
    • 2011-02-19
    • 1970-01-01
    • 2021-02-28
    • 1970-01-01
    • 1970-01-01
    • 2020-01-03
    • 2020-07-30
    • 2020-07-06
    • 2016-02-08
    相关资源
    最近更新 更多