juniperWhite
juniperWhite

Reputation: 77

how to export modules and variables

Im working on sensor code and when an object is it certain bounds it should emit('enterProximity') so when this code runs

main.js

var entered = require('./main').onProximityBoolean
io.on('connection', function(socket) {
setInterval(function() {
console.log("entered index " + entered);
var here = entered.onProximityBoolean;
if (here == true) {
  socket.emit('enterProximity');
} 
 },1000);
 }

In this code "here" should equal true when "enter" is true in main.js

enter = false
function onProximityBoolean(enter) {
      console.log(enter + " emit entered");
      return enter;
}

module.exports = {
  withinBounds: withinBounds,
  onProximityBoolean: onProximityBoolean(enter)
};

but instead it prints like this

https://i.sstatic.net/Yvh6U.jpg

how do i get here to reassign itself continously?

Upvotes: 1

Views: 63

Answers (1)

intentionally-left-nil
intentionally-left-nil

Reputation: 8336

Your module.exports is returning the value of onProximityBoolean(undefined), rather than the function itself.

If you change your module.exports to this

module.exports = {
  withinBounds: withinBounds,
  onProximityBoolean: onProximityBoolean,
};

and then your runner to this:

var entered = require('./main').onProximityBoolean
io.on('connection', function(socket) {
  setInterval(function() {
    console.log("entered index " + entered);
    var here = entered.onProximityBoolean(entered);
    if (here == true) {
      socket.emit('enterProximity');
    } 
  },1000);
}

does that fix your issue? The change is to make sure that onProximityBoolean is a function, and that you call the function every time in your setInterval loop.

Upvotes: 1

Related Questions