gpt4 book ai didi

javascript - glob-fs glob.readdirSync 的意外行为

转载 作者:行者123 更新时间:2023-12-03 01:05:50 27 4
gpt4 key购买 nike

我有以下nodejs代码。客户端首先调用/api/demosounds,然后调用/api/testsounds

var glob = require('glob-fs')({ gitignore: true });
app.get('/api/demosounds',function(req,res){
var demosounds = []
var demosoundlist = glob.readdirSync('src/assets/demosounds/*.wav');
demosounds = demosounds.concat(demosoundlist)
for (var i = 0; i < demosounds.length; i++) {
demosounds[i] = demosounds[i].replace("src/assets/","/api/static/")
}
demosounds = demosounds.sort()
res.json(demosounds)
})

app.get('/api/testsounds',function(req,res){
var listofsounds = []
var folderlist = ['01_sinus','02_pulse_train','03_contour_le','04_contour_fel','05_variable','06_complex_le','07_complex_fel']

for (var x = 0; x < folderlist.length; x++){
var testsoundlist = glob.readdirSync('src/assets/' + folderlist[x] +'/*.wav');

listofsounds = listofsounds.concat(testsoundlist)
}

在执行 glob.readdirSync('src/assets/01_sinus/*.wav') 时,我希望仅获得以 src/assets/01_sinus/ 开头的路径然而 testsoundlist 的开头如下:

[ 'src/assets/demosounds/electricity.wav',
'src/assets/demosounds/phone.wav',
'src/assets/demosounds/water.wav',
'src/assets/demosounds/wind.wav',
'src/assets/01_sinus/02_sin1_0065_0.16923076923076924.wav',
'src/assets/01_sinus/04_sin1_0065_0.7692307692307693.wav',
'src/assets/01_sinus/05_sin1_0065_1.0615384615384615.wav',
'src/assets/01_sinus/07_sin1_0165_0.07272727272727272.wav',

我不知道为什么会发生这种情况:(

更新

下面的代码更接近问题

var glob = require('glob-fs')({ gitignore: true });
var folderlist = ['01_sinus','02_pulse_train','03_contour_le','04_contour_fel','05_variable','06_complex_le','07_complex_fel']
for (var x = 0; x < folderlist.length; x++){
console.log((glob.readdirSync('src/assets/' + folderlist[x] +'/*.wav').length))
}

输出这个,就好像glob会记住之前的glob一样。

49
98
147
196
245
294
343

最佳答案

图书馆似乎不再维护了,有一个挂issue有着完全相同的问题,所以看起来这是一个错误。

解决方案是简单地使用 glob ,在本例中是glob.sync()

关于javascript - glob-fs glob.readdirSync 的意外行为,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52414684/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com