简体   繁体   中英

load query every second using node js

I want to select data after inserting, so what i want is to check it every second if there is a new data. I create a select query then try to display it using console log. Please help me, I'm a newbie. Here is my code

Controller

var connection = require('../../config/db');

function myQuery(callback) {
this.pushActivity = function(req, res, next) {
    var salesman_id = req.query.salesman_id
    connection.acquire(function(err, con) {
        con.query('SELECT * FROM `activities` where salesman_id="'+salesman_id+'" and kunci="0" and created=NOW()', function(err, data) {
            con.release();
            if (err) {
                return res.json({ status: 400, message: 'Failed', result: [] });
            } else {
                console.log(data)
                // return res.json(data);
            }
        });
    });
};
callback();
}

function wait10sec(){
 setTimeout(function(){
     myQuery(wait10sec);
 }, 10000);
}

myQuery(wait10sec);

module.exports = new myQuery();

Router

var ad = 
require('./../../controllers/mobile/activity_after_deal');

module.exports = {
   configure: function(app) {
      app.route('/api_ad_push_notif').get(ad.pushActivity);
   }
};

这不是一个好的和有效的方法。而不是使用这个,尝试在更改数据的 put 请求上使用监听器,在更改事件之后,执行 Get 查询并输出更改的数据。为此目的使用Socket.io 库

you can use socket.io . by this module you can send live event to server and get response.

for example when send new data, you can select query or send emit to user or show it on console log

You can create worker by adding

setInterval(function(){        
  //run query and console        
}, 1000) and you can set time        

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM