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.
-
You're sure it's not the first
.DS_Store
that is causing the issue ? – adeneo Commented Dec 19, 2013 at 19:33
1 Answer
Reset to default 9You 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
版权声明:本文标题:javascript - Node.js: Directory has no method 'basename' when path.basename is used - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744099582a2590811.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论