javascript - 我的回调出了什么问题?

标签 javascript node.js callback

这是文件“path-info.js”,有 2 个功能:pathInfo 和回调。 Pathinfo 从对象“info”中的路径收集有关文件的所有信息,回调获取该对象并返回它。代码:

"use strict";
const fs = require("fs");

let getInfo = function (err, someObject) {
    if (err) throw err;
    return someObject;
};

function pathInfo(path, callback) {
  let info = {};

  info.path = path; // write path info

  fs.stat(path, (err, type) => { // write type info
    if (type.isFile()) {
      info.type = "file";
    }
    if (type.isDirectory()) {
      info.type = "directory";
    } else {
      info.type = "undefined";
    }
  });

  fs.stat(path, (err, type) => { // write content info if it is file
    if (type.isFile()) {
      fs.readFile(path, "utf8", (err, content) => {
        info.content = content;
      });
    } else {
      info.content = "undefined";
    }
  });

  fs.stat(path, (err, type) => { // write childs if it is directory
    if (type.isDirectory()) {
      fs.readdir(path, (err, childs) => {
        info.childs = childs
      });
    } else {
      info.childs = "undefined";
    }
  });

  getInfo(null, info); // callback returns object "info"
}

module.exports = pathInfo;

我使用所示的回调函数,例如,此处:nodeJs callbacks simple example 。尽管如此,这段代码仍然不起作用,我也不知道为什么。

我使用文件“test.js”调用此代码,代码如下:

const pathInfo = require('./path-info');
function showInfo(err, info) {
  if (err) {
    console.log('Error occurred');
    return;
  }

  switch (info.type) {
    case 'file':
      console.log(`${info.path} — is File, contents:`);
      console.log(info.content);
      console.log('-'.repeat(10));
      break;
    case 'directory':
      console.log(`${info.path} — is Directory, child files:`);
      info.childs.forEach(name => console.log(`  ${name}`));
      console.log('-'.repeat(10));
      break;
    default:
      console.log('Is not supported');
      break;
  }
}

pathInfo(__dirname, showInfo);
pathInfo(__filename, showInfo);

所以逻辑是我需要为回调提供包含有关目录或文件的一些信息的对象。根据此情况,将显示一些 console.logs。

任何帮助将不胜感激!

UPD:更新了代码,只是将我的“callback”函数重命名为“getInfo”。

最佳答案

回调是作为参数传递给另一个函数的函数。

在您的情况下,您的第二个参数是函数 showInfo ,它是您的回调。您的函数pathInfo接受两个参数,第二个是showInfo

因此,当您调用它时,您会使用一些参数执行 showInfo 中的代码,通常会出错,然后是其他内容。

在您的情况下,您在 showInfo 中将第二个参数命名为“callback”,因此您必须使用要求的参数(err 和 info)来执行它。

示例:

function myfunc (parameter,cb) {
    cb(null,{});
}

myfunc("one", function (err,res) {
    console.log(err);
});

其中“myfunc”中的“cb”是作为第二个参数发送的函数。

可以像你那样写:

var cb = function (err,res) {
    console.log(err);
}

myfunc("one",cb);

关于javascript - 我的回调出了什么问题?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45372544/

相关文章:

javascript - 从回调中获取值(value)

用于 REST 的 Node.js Express? Express 中有 Controller 吗?

node.js - Reactjs 中的 Owl Carousel 2 服务器端渲染

javascript - canvas 上下文 - 缓存 window.requestAnimationFrame 回调函数的变量值

javascript - 如何在返回 "window.onbeforeunload"上的内容之前明显更改 css

node.js - 使用 Node.js SDK 的 AWS 签名不匹配

c# - CallBack on garbagecollected Delegate

javascript - 如何使用转义的 unicode 解码字符串?

javascript - 仅在 IE 中出现 window.open 错误 - 参数无效

javascript - 我需要使用kineticjs单击事件以任何所需的 Angular 旋转 Canvas 中的文本