2017-07-28 37 views
1

这是具有2个功能的文件“path-info.js”: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.log。

任何帮助将不胜感激!

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

+0

定义“不工作' –

+0

这是因为你在你的'path-info.js'中声明了一个名为'callback'的变量,重命名它并且它将解决你的问题 –

+0

你得到了什么错误信息? –

回答

0

如果有人有兴趣。我找到了解决办法和它的作品!正如@ ADreNaLiNe-DJ正确指出的,当我调用getInfo回调来返回信息对象时,我的回调没有完成。所以出路在于改变我的抽象层次:我所做的一切都是将我的回调粘贴到函数中。看到代码:

"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,希望不会打扰你(他们不带来任何价值反正到理解它是如何工作)

0

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

在你的情况下,你的第二个参数是功能showInfo这是你的回调。您的功能pathInfo接受两个参数,第二个是showInfo

所以当你调用它时,你需要执行showInfo中的代码,并带有一些参数,通常是err,然后是其他参数。

在你的情况,你的名字第二个参数“回调”在showInfo,所以你有问参数(ERR和信息)来执行它。

实施例:

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