约翰尼五世:与Heroku联机运行时,开发板方法不起作用

时间:2019-09-13 13:28:06

标签: javascript node.js heroku arduino johnny-five

我正在尝试获取通过arduino读取的数据,以与我使用Heroku创建的网站进行交互。当我在本地服务器上运行它时,它可以正常工作,但是当通过heroku运行它时,没有任何板方法或事件都起作用,我得到:

unhandledRejection TypeError: Cannot read property 'filter' of undefined
at Board.<anonymous> (/app/node_modules/johnny-five/lib/board.js:46:26)
at promise.then.err (/app/node_modules/@serialport/stream/stream.js:664:57)
at process._tickCallback (internal/process/next_tick.js:68:7)
at Function.Module.runMain (internal/modules/cjs/loader.js:834:11)
at startup (internal/bootstrap/node.js:283:19)
at bootstrapNodeJSCore (internal/bootstrap/node.js:622:3)

我曾尝试添加Promise和错误捕获功能,但是现在我想知道该软件是否不兼容。

这是包含Johnny-Five并试图修复错误的代码部分。

const five = require("johnny-five");
const board = new five.Board();

var promise = new Promise(function(resolve, reject) {
    if(board != null && board != undefined && five != null && five != undefined) {
        resolve("Stuff worked!");
    }else {
        reject(Error("It broke"));
    }
});

board.on("ready", function() {
    const motion = new five.Motion({pin: 2, freq:1000});
    // Create a new `motion` hardware instance

    motion.on("calibrated", function() {
        console.log("calibrated");
      });

    // 'Data' contains the motion sensor readings
    motion.on("data", function(data) {
        console.log(data.detectedMotion);
        if (data.detectedMotion) {
            occupied = 'true';
        } else {
            occupied = 'false';
        }
    });
});

promise 
    .then(function(successMessage) { 
        console.log(successMessage); 
    }, function(errorMessage) { 
        console.log(errorMessage); 
    });

process.on('unhandledRejection', error => {
    console.log('unhandledRejection', error);
});

诺言已经解决,程序也不会崩溃,但是我什至没有收到任何数据

board.on("info", function(event) {

  console.log("%s sent an 'info' message: %s", event.class, event.message);
});

board.info("Board", "I got somethin' to say!", { foo: 1 });

0 个答案:

没有答案