skip to Main Content
var query = `select * from map_balances where balance_userid = $1 AND balance_vendorid = $2 AND balance_amount >= $3`
                var values = [accesstoken,token,arr.map(e=>e.vid),arr.map(e=>e.amt)];
                client.query(query,{arr:values},async(err,projectdata)=>{
                    if (err) {
                        res.json({success:false,message:err.message,responsecode:-1});
                    } else {
                        res.json({success:true,message:projectdata.rowCount,responsecode:200});
                    }
                });

here arr=[{"vid": 636,"amt": "21","add": "123123"},{"vid": 636,"amt": "21","add": "123123"},{"vid": 636,"amt": "21","add": "123123"}] will be like this from client

here I am trying to the get data from db, arr is an array which contains no.of objects in each object "amt" is there and it should be greater then the balance in db
I am using postgres and tried some other types as well still not working

2

Answers


  1. Chosen as BEST ANSWER
    let _final = [];
    _arr.forEach(e => {
        _final.push([userid, e.add, e.amt, e.vid, "NOW()"]);
    })
    let query1 = format(`INSERT INTO public.map_mass_payouts (mp_user_id, mp_receiver_address, mp_receiver_amount, mp_receiver_vendorid ,mp_date) values %L returning mp_id`, _final);
    // console.log(query1);
    client.query(query1, [], async (err, data) => {
        res.json({ success: true, responsecode: 200, message: "Request Received Successfully", _status });
    })
    

    and install npm i pg-format...


  2. If you pass the array json itself to postgreSQL it would look like:

    var query = `select mb.*
    from map_balances mb
        inner join jsonb_to_recordset( $1::jsonb) as x(vid int, amt int, add int)
        on mb.balance_vendorid = x.vid AND
           mb.balance_amount >= x.amt AND
           mb.balance_userid = x.add;`
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search