admin管理员组

文章数量:1394526

I have an array of files, and I am trying to get just the basename of the files, without their long extensions. Here is an example of what the array looks like:

[ 
  '/public/uploads/contentitems/.DS_Store',
  '/public/uploads/contentitems/063012A5-60BC-4A4C-AEC2-56B0D5D99EF0/063012A5-60BC-4A4C-AEC2-56B0D5D99EF0.png',
  '/public/uploads/contentitems/063012A5-60BC-4A4C-AEC2-56B0D5D99EF0/063012A5-60BC-4A4C-AEC2-56B0D5D99EF0_1.png',
  '/public/uploads/contentitems/2A431412-A776-4D11-841A-B640DF37C9E2/2A431412-A776-4D11-841A-B640DF37C9E2_2.png' 
]

I want to get:

[ 
  '.DS_Store',
  '063012A5-60BC-4A4C-AEC2-56B0D5D99EF0/063012A5-60BC-4A4C-AEC2-56B0D5D99EF0.png',
  '063012A5-60BC-4A4C-AEC2-56B0D5D99EF0/063012A5-60BC-4A4C-AEC2-56B0D5D99EF0_1.png',
  '2A431412-A776-4D11-841A-B640DF37C9E2/2A431412-A776-4D11-841A-B640DF37C9E2_2.png' 
]

According to the docs, the path.basename function should return me the file without the path.

But what I get is the following error:

TypeError: Object /public/uploads/contentitems has no method 'basename'

Here is the code I am using right now:

    var walk = function(dir, done) {
        var results = [];
        fs.readdir(dir, function(err, list) {
            if (err) return done(err);
            var pending = list.length;
            if (!pending) return done(null, results);
            list.forEach(function(file) {
                file = dir + '/' + file;
                fs.stat(file, function(err, stat) {
                    if (stat && stat.isDirectory()) {
                        walk(file, function(err, res) {
                            results = results.concat(res);
                            if (!--pending) done(null, results);
                        });
                    } else {
                        var suffix = getSuffix(file);
                        if (!verObj[suffix]) results.push(file);
                        if (!--pending) done(null, results);
                    }
                });
            });
        });
    };

    walk(path, function(err, results) {
        if (err) throw err;
        results.forEach(function(file) {
            console.log(path.basename(file));
        });
        self.respond({files: results}, {format: 'json'});
    });

I am using path = require('path'); at the top of my file as well.

I have an array of files, and I am trying to get just the basename of the files, without their long extensions. Here is an example of what the array looks like:

[ 
  '/public/uploads/contentitems/.DS_Store',
  '/public/uploads/contentitems/063012A5-60BC-4A4C-AEC2-56B0D5D99EF0/063012A5-60BC-4A4C-AEC2-56B0D5D99EF0.png',
  '/public/uploads/contentitems/063012A5-60BC-4A4C-AEC2-56B0D5D99EF0/063012A5-60BC-4A4C-AEC2-56B0D5D99EF0_1.png',
  '/public/uploads/contentitems/2A431412-A776-4D11-841A-B640DF37C9E2/2A431412-A776-4D11-841A-B640DF37C9E2_2.png' 
]

I want to get:

[ 
  '.DS_Store',
  '063012A5-60BC-4A4C-AEC2-56B0D5D99EF0/063012A5-60BC-4A4C-AEC2-56B0D5D99EF0.png',
  '063012A5-60BC-4A4C-AEC2-56B0D5D99EF0/063012A5-60BC-4A4C-AEC2-56B0D5D99EF0_1.png',
  '2A431412-A776-4D11-841A-B640DF37C9E2/2A431412-A776-4D11-841A-B640DF37C9E2_2.png' 
]

According to the docs, the path.basename function should return me the file without the path.

But what I get is the following error:

TypeError: Object /public/uploads/contentitems has no method 'basename'

Here is the code I am using right now:

    var walk = function(dir, done) {
        var results = [];
        fs.readdir(dir, function(err, list) {
            if (err) return done(err);
            var pending = list.length;
            if (!pending) return done(null, results);
            list.forEach(function(file) {
                file = dir + '/' + file;
                fs.stat(file, function(err, stat) {
                    if (stat && stat.isDirectory()) {
                        walk(file, function(err, res) {
                            results = results.concat(res);
                            if (!--pending) done(null, results);
                        });
                    } else {
                        var suffix = getSuffix(file);
                        if (!verObj[suffix]) results.push(file);
                        if (!--pending) done(null, results);
                    }
                });
            });
        });
    };

    walk(path, function(err, results) {
        if (err) throw err;
        results.forEach(function(file) {
            console.log(path.basename(file));
        });
        self.respond({files: results}, {format: 'json'});
    });

I am using path = require('path'); at the top of my file as well.

Share Improve this question asked Dec 19, 2013 at 19:28 NeilNeil 2,5197 gold badges34 silver badges47 bronze badges 1
  • You're sure it's not the first .DS_Store that is causing the issue ? – adeneo Commented Dec 19, 2013 at 19:33
Add a ment  | 

1 Answer 1

Reset to default 9

You have a variable name conflict with path.

Use some something else other than path to represent the directory you want to walk.

walk(path, function(err, results) {

You are passing a string named path into walk().

console.log(path.basename(file));  // <-- path is a string here

本文标签: javascriptNodejs Directory has no method 39basename39 when pathbasename is usedStack Overflow