我的回拨有什么问题?

这是具有2个函数的文件“path-info.js”:pathInfo&callback。 Pathinfo从对象“info”中收集有关文件的所有信息,callback获取该对象并将其返回。 码:

"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; 

我使用我的callback函数,例如,在这里: nodeJscallback简单的例子 。 不过,这个代码不起作用,我不知道为什么。

我使用文件“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); 

所以逻辑是我需要给我的callback包含目录或文件的一些信息的对象。 根据这一点,一些console.logs将被显示。

任何帮助将不胜感激!

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

callback函数是您作为parameter passing给另一个函数的函数。

你的情况,你的第二个参数是函数showInfo这是你的callback。 你的函数pathInfo接受两个参数,其次是showInfo

所以当你调用它的时候,你可以在showInfo中用一些参数执行代码,通常是err,然后是其他的参数。

在你的情况下,你在showInfo中命名第二个参数“ callback ”,所以你必须用被询问的参数(err和info)来执行它。

例:

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

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

它可以像你这样写:

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

如果有人感兴趣..我find了一个解决scheme,它的工作原理! 正如@ ADreNaLiNe-DJ正确地指出,当我调用getInfocallback来返回信息对象时,我的callback没有完成。 所以出路在于改变我的抽象层次:我所做的一切都是把我的callback粘贴到函数中。 看到这个代码:

 "use strict"; const fs = require("fs"); let pathInfo = (path, callback) => { let info = {}; info.path = path; fs.stat(path, (err, type) => { if (err) throw err; if (type.isFile()) { info.type = "file"; fs.readFile(path, "utf8", (err, content) => { info.content = content; info.childs = undefined; callback(err, info); }); } if (type.isDirectory()) { info.type = "directory"; fs.readdir(path, (err, childs) => { info.childs = childs; info.content = undefined; callback(err, info); }); } }); }; let showInfo = (err, info) => { // Отсюда и ниже вставлен код из текста if (err) { // из домашнего задания console.log('Возникла ошибка при получении информации'); return; } switch (info.type) { case 'file': console.log(`${info.path} — является файлом, содержимое:`); console.log(info.content); console.log('-'.repeat(10)); break; case 'directory': console.log(`${info.path} — является папкой, список файлов и папок в ней:`); info.childs.forEach(name => console.log(` ${name}`)); console.log('-'.repeat(10)); break; default: console.log('Данный тип узла не поддерживается'); break; } }; pathInfo(__dirname, showInfo); pathInfo(__filename, showInfo); 

PS:抱歉俄罗斯console.logs,希望它不会打扰你(他们没有带来任何价值无论如何理解它是如何工作的)