これは、ラズベリーパイの/dev
フォルダからすべてのUSBドライブを解析する機能です。 sda
、ada1
、sdb
、sdb1
を配列として返したいが、失敗する。 console.log(readDeviceList())
を実行しても何も印刷されません。私のコードに何が問題なのですか?JavaScriptコードが配列を返しません
var usbDeviceList = new Array();
function readDeviceList() {
var usbDeviceList = new Array();
fs.readdir(deviceDir, function (error, file) {
if (error) {
console.log("Failed to read /dev Directory");
return false;
} else {
var usbDevCounter = 0;
console.log("Find below usb devices:");
file.forEach(function (file, index) {
if (file.indexOf(usbDevicePrefix) > -1) {
usbDeviceList[usbDevCounter++] = file;
}
});
console.log(usbDeviceList); // This prints out the array
};
});
console.log(usbDeviceList); // This does not print out the array
return usbDeviceList; // Is this return value valid or not?
}
あなたがusbDevicePrefixを定義してくださいどこに? – Nick
[非同期呼び出しからの応答を返すにはどうすればよいですか?](https://stackoverflow.com/questions/14220321/how-do-i-return-the-response-from-an-asynchronous-call) – PMV
@snapjsはい、私はそれを上に定義しました。そのコードを実行するとエラーはありません – eric