1

I am a beginner for Node.js. Please help to fix this.

Without giving WHERE clause update is working fine. Below is the script without "WHERE" clause:

var post  = {name: 'Amit', mobile:'123456'};
var query = connection.query('UPDATE contacts SET ? ', post , function(err, result) {});
console.log(query.sql);

Output:

enter image description here

Now I added 'WHERE' clause..getting error:

var post  = {name: 'Amit', mobile:'123456'};
var condition = {id:4};
var query = connection.query('UPDATE contacts SET ? ',post,' WHERE '+ condition , function(err, result) {});
console.log(query.sql);

Output:

enter image description here

1
  • Try change 'WHERE '+condition with 'WHERE id='+condition.id Commented May 8, 2013 at 9:37

4 Answers 4

3

According to the api, your should write you query like this:

connection.query('UPDATE contacts SET Name = ?,Mobile=? WHERE yourCondition = ?', [post.name,post.mobile,condition], function(err, result) {})
Sign up to request clarification or add additional context in comments.

Comments

1

Try that code:

var post  = {name: 'Amit', mobile:'123456'};
var condition = {id:4};
var query = connection.query('UPDATE contacts SET ? WHERE ?', [post, condition] , function(err, result) {});
console.log(query.sql);

Comments

0

try this ,it workd for me

connection.query('UPDATE nodeDB.USER SET USER_PASSWORD :Pass WHERE USER_NAME :Name',
  {Name: 'max',Pass: '123'}, function(err, rows) {
}); 

Comments

0

Check My code also work for you.

router.all('/setLoginDetails', function (req, res) {
    if(req.method == 'POST')
    {
        var Req = req.body;
    }
    else
    {
        var Req = req.query;
    }
    if(Req.lang == undefined || Req.lang == 'en')
    {
        const message = require('../lang/messages_en.json');
    }

    if(Req.id == undefined || Req.id == '')
    {
        return res.json({"success" : false, "Message" : message.REQUIRED , "data" : {} });
    }

    qb.select('id')
        .where({id: Req.id})
        .get('table', (err,rows) => {
            if (err || rows.length == 0)
            {

                return res.json({"success" : false, "Message" : "No data found", "data" : Response});
            }
            else
            {
                _.each(rows, function(record) {
                    var token = tokenGenerate();
                    qb.update('token', {token: token}, {driver_id:record.id}, (err, result) => {
                        if (err) return console.error(err);
                    });

                    qb.select('*').where({driver_id: record.id}).get(model.DriverLogin, (err,rows) => {
                        var lat = Req.lat;
                        var lng = Req.lng;
                        if(Req.lat == '')
                        {
                            lat = 0.0;
                        }
                        if(Req.lng == '')
                        {
                            lng = 0.0;
                        }
                        var updateData = {lat : lat ,lng : lng, status : 'free', screen : Req.device_info, driver_id : record.id};
                        if(rows.length > 0)
                        {
                            qb.update('localtion', updateData, {driver_id:record.id}, (err, result) => {
                                if (err) return console.error(err);
                            });
                        }
                        else
                        {
                            qb.insert(model.DriverLogin, updateData, (err, res) => {
                                if (err) return console.error(err);
                            });
                        }
                    });
                });

                return res.json({"success" : true, "Message" : "Data Updated", "data" : Response});
            }
        }
    );
});

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.