1

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?

3
  • You are calling the function onProximityBoolean in module.exports with enter which from the code posted is undefined. Which assigns the key onProximityBoolean with a value false and it never changes. Commented Jun 27, 2017 at 17:42
  • i forgot to add that i had defined it, i edited it thanks Commented Jun 27, 2017 at 17:47
  • Even with the update. the variable enter is never changing as you are not calling onProximityBoolean inside setInterval. Further More, since module.exports = { withinBounds: withinBounds, onProximityBoolean: onProximityBoolean(enter) }; has an assigned value false for the key onProximityBoolean Commented Jun 27, 2017 at 17:51

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.