Skip to content

[Bug]: readdir wirft Fehler #1939

Open
Open

Description

@ente34

I'm sure that

  • This issue is still present in the current beta version of this adapter
  • There is no other (open) issue with the same topic (use the search!)
  • 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

Activity

  1. Apollon77 commented on Jul 27, 2025

    @Apollon77
    Contributor

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

  2. ente34 commented on Jul 28, 2025

    @ente34
    Author

    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 '...'

  3. added theissue type on Jul 29, 2025
  4. GermanBluefox commented on Jul 29, 2025

    @GermanBluefox
    Contributor
    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));
  5. removed theissue type on Jul 29, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions