ioBroker.javascript icon indicating copy to clipboard operation
ioBroker.javascript copied to clipboard

[Bug]: readdir wirft Fehler

Open ente34 opened this issue 5 months ago • 3 comments

I'm sure that

  • [x] This issue is still present in the current beta version of this adapter
  • [x] There is no other (open) issue with the same topic (use the search!)
  • [x] This issue is not described in the adapter documentation / FAQ (read the docs!)

Script type

JavaScript

The problem

const fs = require('fs');
const util = require('util');
const my_readdir = util.promisify(fs.readdir);

const searchDir = '/tmp';

async function FindFile(dir) {
	let rc = '';
	try {
		let files = [];
		files = await my_readdir(dir);
		for (let file of files) {
			let fullpath = dir + '/' + file;
			rc = fullpath;
		}
	} catch (e) {
		console.log(e);
	} finally {
		return rc;
	}
}

function execPromise(command) {
	return new Promise(function(resolve, reject) {
		exec(command, (error, stdout, stderr) => {
			if (error) {
				//console.log(stderr);
				reject(error);
				return;
			}
			resolve(stdout.trim());
		});
	});
}

try {
	let result = FindFile(searchDir);
	//let result = await execPromise(find ${searchDir} -type f );
	log(result);
} catch (e) {
	console.error(e.message);
}

iobroker.current.log (in debug mode!)

javascript.0 2025-07-27 11:42:01.749 info script.js.Test.readdir: TypeError: Cannot read properties of undefined (reading 'ProtectFs') at readdir (/opt/iobroker/node_modules/iobroker.javascript/src/lib/protectFs.ts:625:14) at node:internal/util:438:7 at new Promise (<anonymous>) at readdir (node:internal/util:424:12) at FindFile (script.js.Test.readdir:114:23) at script.js.Test.readdir:142:18 at script.js.Test.readdir:152:3 at Script.runInContext (node:vm:149:12) at Script.runInNewContext (node:vm:154:17) at JavaScript.execute (/opt/iobroker/node_modules/iobroker.javascript/src/main.ts:2203:27)

Version of nodejs

20.19.1

Version of ioBroker js-controller

7.0.7

Version of adapter

9.0.8

ente34 avatar Jul 27 '25 10:07 ente34

Why you promisify yourself? JUst use the promise base functions from Node.js

Apollon77 avatar Jul 27 '25 17:07 Apollon77

Why you promisify yourself? JUst use the promise base functions from Node.js

a code snippet (working in javascript script) would be appreciated, i.e the correct require '...'

ente34 avatar Jul 28 '25 13:07 ente34

const fs = require('fs');
const my_readdir = path => new Promise((resolve, reject) =>
     fs.readdir(path, (error, result) => error ? reject(error) : resolve(result)));

// Same as await
my_readdir('.').then(res => console.log(res));

GermanBluefox avatar Jul 29 '25 11:07 GermanBluefox