TypeError:callback不是nodejs中的函数

时间:2017-02-14 04:52:43

标签: node.js

我正在尝试学习nodejs并偶然发现此错误

TypeError:回调不是函数

当我尝试使用此命令调用服务器时。

curl http://localhost:8090/albums.json

这是我的server.js

的代码



var http = require('http'),
  fs = require('fs');

function load_album(album_name, callback) {
  fs.readdir("albums/", +album_name, (err, files) => {
    if (err) {
      if (err.code == "ENOENT") {
        callback(make_error("no_such_album", "That album doesn't exist"));
      } else {
        callback(make_error("can't load album", "The server is broken"));
      }
    } else {
      //callback(null, files);
      var only_files = [];
      var path = 'albums/${album_name}/';

      var iterator = (index) => {
        if (index == files.length) {
          var obj = {
            short_name: album_name,
            photos: only_files
          };
          callback(null, obj);
          return;
        }

        fs.stat(path + files[index], (err, stats) => {
          if (!err && stats.isFile()) {
            only_files.push(files[index]);
          }
          iterator(index + 1);
        });
      };
      iterator(0);
    }
  });
}


function handle_incoming_request(req, res) {
  console.log("incoming request: " + req.method + " " + req.url);

  if (req.url == '/albums.json') {
    load_album((err, albums) => {
      if (err) {
        res.writeHead(500, {
          "Content-Type": "application/json "
        });
        res.end(JSON.stringify({
          code: "cant_load_albums",
          message: err.message
        }));
      } else {
        var output = {
          error: null,
          data: {
            albums: albums
          }
        };
        res.writeHead(200, {
          "Content-Type": "application/json"
        });
        res.end(JSON.stringify(output) + "\n");
      }
    });
  } else if (req.url.substr(0, 7) == '/albums' && req.url.substr(req.url.length - 5) == '.json') {
    //user is requesting contents of album
    load_album(req.url.substr(7, req.url.length - 12), (err, photos) => {
      if (err) {
        res.writeHead(500, {
          "Content-type": "application/json"
        });
        res.end(JSON.stringify(err));
      } else {
        var output = {
          error: null,
          data: {
            photos: photos
          }
        };
        res.writeHead(200, {
          "Content-Type": application / json
        });
        res.end(JSON.stringify(output) + "\n");
      }
    });
  } else {
    res.writeHead(404, {
      "Content-type": "application/json"
    });
    res.end(JSON.stringify({
      code: "no_such_page",
      message: "No such page"
    }));
  }
}
var s = http.createServer(handle_incoming_request);
s.listen(8090);




你能告诉我我的代码出了什么问题,我收到错误告诉我回调不是一个功能吗?

谢谢

对于更多格式化代码,您可以访问https://jsfiddle.net/02dbx6m9/

2 个答案:

答案 0 :(得分:5)

var http = require('http'),
  fs = require('fs');

function load_album(album_name, callback) {
  fs.readdir("albums/", +album_name, (err, files) => {
    if (err) {
      if (err.code == "ENOENT") {
        callback(make_error("no_such_album", "That album doesn't exist"));
      } else {
        callback(make_error("can't load album", "The server is broken"));
      }
    } else {
      //callback(null, files);
      var only_files = [];
      var path = 'albums/${album_name}/';

      var iterator = (index) => {
        if (index == files.length) {
          var obj = {
            short_name: album_name,
            photos: only_files
          };
          callback(null, obj);
          return;
        }

        fs.stat(path + files[index], (err, stats) => {
          if (!err && stats.isFile()) {
            only_files.push(files[index]);
          }
          iterator(index + 1);
        });
      };
      iterator(0);
    }
  });
}


function handle_incoming_request(req, res) {
  console.log("incoming request: " + req.method + " " + req.url);

  if (req.url == '/albums.json') {
    load_album("ALBUM NAME", (err, albums) => {
      if (err) {
        res.writeHead(500, {
          "Content-Type": "application/json "
        });
        res.end(JSON.stringify({
          code: "cant_load_albums",
          message: err.message
        }));
      } else {
        var output = {
          error: null,
          data: {
            albums: albums
          }
        };
        res.writeHead(200, {
          "Content-Type": "application/json"
        });
        res.end(JSON.stringify(output) + "\n");
      }
    });
  } else if (req.url.substr(0, 7) == '/albums' && req.url.substr(req.url.length - 5) == '.json') {
    //user is requesting contents of album
    load_album("Album Name", req.url.substr(7, req.url.length - 12), (err, photos) => {
      if (err) {
        res.writeHead(500, {
          "Content-type": "application/json"
        });
        res.end(JSON.stringify(err));
      } else {
        var output = {
          error: null,
          data: {
            photos: photos
          }
        };
        res.writeHead(200, {
          "Content-Type": application / json
        });
        res.end(JSON.stringify(output) + "\n");
      }
    });
  } else {
    res.writeHead(404, {
      "Content-type": "application/json"
    });
    res.end(JSON.stringify({
      code: "no_such_page",
      message: "No such page"
    }));
  }
}
var s = http.createServer(handle_incoming_request);
s.listen(8090);

您忘记在load_album方法中传递相册名称参数。这就是为album_name参数分配实际回调的原因,而回调参数仍未定义。

答案 1 :(得分:2)

以下是问题的根本原因:

load_album((err, albums) => {
    // ...
});

该功能的签名需要两个参数,但您只能传递第一个参数:

function load_album(album_name, callback) {}

其中,一旦被调用,callback将被取消定义,但您仍然试图将其视为可调用对象。这是一个更简洁的例子,说明如何重现错误:



function foo(bar, baz) {
  baz() 
}

foo(() => {})