admin管理员组文章数量:1333451
I am having a strange error with NodeJS when I call the function pop()
on an array, it says TypeError: cars.pop is not a function
... I am confused.
Any help? Below is the code. Thank you,
//callback chaining to avoid having multiple callbacks in the event queue
//only one callback calling others
function showCar(car, callback) {
console.log('Saw a ' + car);
if (car.length) {
//register the function as asynchronous
process.nextTick(function() {
callback();
})
}
}
function logCars(cars) {
var car = cars.pop();
showCar(car, function() { //chaining of call backs
logCars(car);
});
}
var cars = ['ferrari', 'porsh', 'Hyundai', 'Peugeot'];
logCars(cars);
I am having a strange error with NodeJS when I call the function pop()
on an array, it says TypeError: cars.pop is not a function
... I am confused.
Any help? Below is the code. Thank you,
//callback chaining to avoid having multiple callbacks in the event queue
//only one callback calling others
function showCar(car, callback) {
console.log('Saw a ' + car);
if (car.length) {
//register the function as asynchronous
process.nextTick(function() {
callback();
})
}
}
function logCars(cars) {
var car = cars.pop();
showCar(car, function() { //chaining of call backs
logCars(car);
});
}
var cars = ['ferrari', 'porsh', 'Hyundai', 'Peugeot'];
logCars(cars);
Share
Improve this question
edited Jan 30, 2017 at 3:40
Josh Crozier
241k56 gold badges400 silver badges313 bronze badges
asked Jan 30, 2017 at 0:28
CelaroCelaro
2262 gold badges8 silver badges19 bronze badges
1 Answer
Reset to default 4It's because you aren't passing an array to the logCars
function on the second call. You are passing the popped string on the second recursive call.
In other words, logCars(car)
should be logCars(cars)
where you are nesting callbacks:
function logCars (cars){
var car = cars.pop();
showCar(car, function () {
logCars(cars); // This should be `cars`, not `car` like you had
});
}
本文标签: javascriptpop() is not a functionnodejsStack Overflow
版权声明:本文标题:javascript - pop() is not a function - nodejs - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742353132a2458887.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论