store the mouse position data into mongodb

19 Views Asked by At
// // add mouse cursor position. 
var mouse_cord = []
let handleMousemove = (event) => {
  mouse_cord.append((event.x, event.y));
};

let throttle = (func, delay) => {
    let prev = Date.now() - delay;
  
  return (...args) => {
    let current = Date.now();
    if (current - prev >= delay) {
        prev = current;
      func.apply(null, args);
    }
  }
};
document.addEventListener('mousemove', throttle(handleMousemove, 500));

var inputData = "mouse_position";
var xhr = new XMLHttpRequest();
xhr.open("POST", "/raw_data");
xhr.setRequestHeader("Content-Type", "application/json;charset=UTF-8");
xhr.send(JSON.stringify(
    {data: inputData, mouse_raw: mouse_cord}));

@app.route('/raw_data', methods=['POST'])
def raw_data():
    mouse_raw = request.json.get('mouse_raw')
    data = request.json.get('data')
    mongo.db.testagain.insert_many(
        [dict(mouse_raw = mouse_raw,
        data = data,
        Prolific_ID=prolific_id)])
    return 'data received'

I tried to track the mouse coordinate pair in the mongodb database but failed to record anything. Above is my code, is anything wrong with http format please?

// // add mouse cursor position. 
var mouse_cord = []
let handleMousemove = (event) => {
  mouse_cord.append((event.x, event.y));
};

let throttle = (func, delay) => {
    let prev = Date.now() - delay;
  
  return (...args) => {
    let current = Date.now();
    if (current - prev >= delay) {
        prev = current;
      func.apply(null, args);
    }
  }
};
document.addEventListener('mousemove', throttle(handleMousemove, 500));

var inputData = "mouse_position";
var xhr = new XMLHttpRequest();
xhr.open("POST", "/raw_data");
xhr.setRequestHeader("Content-Type", "application/json;charset=UTF-8");
xhr.send(JSON.stringify(
    {data: inputData, mouse_raw: mouse_cord}));

@app.route('/raw_data', methods=['POST'])
def raw_data():
    mouse_raw = request.json.get('mouse_raw')
    data = request.json.get('data')
    mongo.db.testagain.insert_many(
        [dict(mouse_raw = mouse_raw,
        data = data,
        Prolific_ID=prolific_id)])
    return 'data received'

I tried to store mouse coordinate positions in the database but it failed to load in ... anything can help please? Above is my code, does it seems something wrong with http request thing please?

0

There are 0 best solutions below