未捕获(承诺)TypeError:spawn不是函数

时间:2017-03-01 10:28:59

标签: javascript reactjs asynchronous redux react-redux

我有点被困在这里。我在我的动作创建器中使用redux-thunk提交异步请求,如下所示:

export const downloadFromYoutube = (download) => {
  console.log("Hello");
    return dispatch => {
      var YoutubeMp3Downloader = require('youtube-mp3-downloader');

      var YD = new YoutubeMp3Downloader({
          "ffmpegPath": "/usr/local/Cellar/ffmpeg/3.2.2/bin/ffmpeg",        // Where is the FFmpeg binary located?
          "outputPath": "/Users/dominik/Coding/youtube-downloader-papa/downloads",    // Where should the downloaded and encoded files be stored?
          "youtubeVideoQuality": "highest",       // What video quality should be used?
          "queueParallelism": 2,                  // How many parallel downloads/encodes should be started?
          "progressTimeout": 2000                 // How long should be the interval of the progress reports
      });

      console.log("Hello");
      YD.on("finished", data => dispatch({ type: "FINISHED", payload: data }));
      YD.on("error", error => dispatch({ type: "ERROR", payload: error }));
      YD.on("progress", progress => dispatch({ type: "PROGRESS", payload: progress }));

      // dispatch a "starting" action
      // dispatch({ type: "STARTING" });

      // start the download
      YD.download("UDzGLMLhy80");
     }
  };

但我总是收到以下错误:

  

TypeError:spawn不是函数

我在电子中使用React / Redux。我还应该提到redux-thunk在我使用超时功能而不是下载功能时工作。但是,上面的代码&当我将它放在普通的javascript / nodejs文件中并执行它时,使用youtube下载程序库也可以正常工作,所以它似乎与我在React中处理promises的方式有关。

以下是完整的错误日志:

  

未捕获(承诺)TypeError:spawn不是函数(...)(匿名   函数)@ processor.js:135proto._getFfmpegPath @   capabilities.js:90proto._spawnFfmpeg @   processor.js:115proto.availableFormats.proto.getAvailableFormats @   capabilities.js:514(匿名函数)@ capabilitiess.js:564fn @   async.js:746(匿名函数)@async.js:1213(匿名函数)@   async.js:166(匿名函数)@async.js:706(匿名函数)@   async.js:167async.waterfall @ async.js:710proto._checkCapabilities @   capabilities.js:561(匿名函数)@ processor.js:287fn @   async.js:746(匿名函数)@async.js:1213(匿名函数)@   async.js:166(匿名函数)@async.js:706(匿名函数)@   async.js:167async.waterfall @ async.js:710proto._prepare @   processor.js:284proto.exec.proto.execute.proto.run @   processor.js:420proto.saveToFile.proto.save @ recipes.js:28(匿名   功能)@ YoutubeMp3Downloader.js:151emitOne @ events.js:77emit @   events.js:169(匿名函数)@ index.js:113emitOne @   events.js:77emit @ events.js:169ClientRequest._connect @   request.js:235(匿名函数)@ request.js:128

2 个答案:

答案 0 :(得分:1)

Spawn是nodejs的构造。作为框架的React没有方法。该库将在服务器端(nodejs)中使用。您可以公开可以通过react触发的http api。然后,http api可以在nodejs服务器中触发库。

答案 1 :(得分:0)

摘自this文章:

使用Socket.IO

服务器端代码

var io = require('socket.io').listen(80); // initiate socket.io server

io.sockets.on('connection', function (socket) {
  socket.emit('news', { hello: 'world' }); // Send data to client

  // wait for the event raised by the client
  socket.on('my other event', function (data) {  
    console.log(data);
  });
});

和客户端:

<script src="/socket.io/socket.io.js"></script>
<script>
  var socket = io.connect('http://localhost'); // connec to server
  socket.on('news', function (data) { // listen to news event raised by the server
    console.log(data);
    socket.emit('my other event', { my: 'data' }); // raise an event on the server
  });
</script>