[英]Missing error handler on `socket` TypeError: task is not a function - node.js async.js
I need your help with my webserver controlled robot project. 我需要由我的网络服务器控制的机器人项目的帮助。 I try to control two motors via website. 我尝试通过网站控制两个电机。
A lot of stuff is already working so I can open a website and hit the "w" key and both Motors start to run forward. 很多东西已经在工作,所以我可以打开一个网站并按“ w”键,两个Motors都可以开始运行。 (MyControlls: w = Forward, s = backward, a = left, d = right) (MyControlls:w =前进,s =向后,a =左,d =右)
But another command is not processed anymore. 但是不再处理另一个命令。
On command line I can see this error message: 在命令行上,我可以看到以下错误消息:
Missing error handler on `socket`.
TypeError: task is not a function
at /home/pi/tank/node_modules/async/dist/async.js:5285:13
at replenish (/home/pi/tank/node_modules/async/dist/async.js:871:21)
at /home/pi/tank/node_modules/async/dist/async.js:881:15
at _parallel (/home/pi/tank/node_modules/async/dist/async.js:5284:9)
at parallelLimit (/home/pi/tank/node_modules/async/dist/async.js:5317:14)
at Object.parallel (/home/pi/tank/node_modules/async/dist/async.js:930:20)
at Object.tank.moveForward (/home/pi/tank/app.js:46:9)
at Socket.<anonymous> (/home/pi/tank/app.js:87:9)
at emitOne (events.js:96:13)
at Socket.emit (events.js:188:7)
It seem that the problem is around the moveForward : function() and async.parallel (The other Move-functions have the same issue.) 看来问题出在moveForward:function()和async.parallel(其他Move函数也有同样的问题。)
var tank = {
//we create an object with the used pins
motors : {
leftFront: 11,
leftBack: 12,
rightFront: 15,
rightBack: 16
},
//we open the gpio pins and set the as outputs
init : function(){
gpio.open(this.motors.leftFront, "output");
gpio.open(this.motors.leftBack, "output");
gpio.open(this.motors.rightFront, "output");
gpio.open(this.motors.rightBack, "output");
},
//in order to move the tank forward, we supply both motors
moveForward : function(){
async.parallel([
gpio.write(this.motors.leftFront, 1),
gpio.write(this.motors.rightFront, 1)
])
},
//in order to move the tank backwards, we supply both motors, but with inverse polarity
moveBackward : function(){
async.parallel([
gpio.write(this.motors.leftBack, 1),
gpio.write(this.motors.rightBack, 1)
]);
},
//in order to turn right, we supply on the left
moveLeft : function(){
gpio.write(this.motors.leftFront, 1);
},
//in order to turn left, we supply on the right
moveRight : function(){
gpio.write(this.motors.rightFront, 1);
},
//in order to stop both motors, we set all pins to 0 value
stop : function(){
async.parallel([
gpio.write(this.motors.leftFront, 0),
gpio.write(this.motors.leftBack, 0),
gpio.write(this.motors.rightFront, 0),
gpio.write(this.motors.rightBack, 0)
]);
}
};
Please help! 请帮忙!
You're not using async.parallel()
correctly. 您没有正确使用async.parallel()
。 It takes an array of functions, and gpio.write()
does not return a function. 它需要一个函数数组,并且gpio.write()
不返回函数。
You need to do something like this: 您需要执行以下操作:
var self = this;
async.parallel([
function (callback) {
gpio.write(self.motors.leftBack, 1, callback);
},
function (callback) {
gpio.write(self.motors.rightBack, 1, callback);
}
]);
In order to avoid the extra boilerplate, you could also factor out a function that creates the necessary functions as needed: 为了避免多余的样板,您还可以分解出一个函数,该函数可根据需要创建必要的函数:
write: function (motor, value) {
return function (callback) {
gpio.write(motor, value, callback);
};
},
moveForward : function(){
async.parallel([
this.write(this.motors.leftFront, 1),
this.write(this.motors.rightFront, 1)
]);
},
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.