ioBroker / ioBroker.javascript

Script engine for JavaScript and Blockly
MIT License
325 stars 120 forks source link

writeFile - missing meta object when using null as first parameter #1042

Closed klein0r closed 2 years ago

klein0r commented 2 years ago

Describe the bug

Documentation tells me, that I can use writeFile without the first parameter

https://github.com/ioBroker/ioBroker.javascript/blob/d3b21372e1789ea607612bcacb1ef7300e45f386/docs/en/javascript.md?plain=1#L1258-L1262

But: This will not work, because the adapter class will use the adapter name as first parameter (meta object) when value is null. Test Script:

writeFile(null, 'test.txt', 'Hallo Welt', (err) => {
    if (err) {
        console.error(`Unable to write file: ${err}`);
    } else {
        console.log('ok');
    }
});

Error

javascript.0 (1636) script.js.test.testWriteFile: Unable to write file: Error: javascript is not an object of type "meta"

Solution 1

Update the writeFile function in the sandbox, which sets adapter = '0_userdata.0' when adapter is null, because 0_userdata.0 is already an existing object of type meta. Plus: Files created in this meta storage will be visible in the files section of admin.

{
  "_id": "0_userdata.0",
  "type": "meta",
  "common": {
    "icon": "data:image/svg+xml;base64,PHN2ZyB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciIGhlaWdodD0iMjRweCIgdmlld0JveD0iMCAwIDI0IDI0IiB3aWR0aD0iMjRweCI+DQogICAgPGcgZmlsbD0iY3VycmVudENvbG9yIj4NCiAgICAgICAgPHBhdGggZD0iTTE5LDV2MTRINVY1SDE5IE0xOSwzSDVDMy45LDMsMywzLjksMyw1djE0YzAsMS4xLDAuOSwyLDIsMmgxNGMxLjEsMCwyLTAuOSwyLTJWNUMyMSwzLjksMjAuMSwzLDE5LDNMMTksM3oiLz4NCiAgICAgICAgPHBhdGggZD0iTTE0LDE3SDd2LTJoN1YxN3ogTTE3LDEzSDd2LTJoMTBWMTN6IE0xNyw5SDdWN2gxMFY5eiIvPg0KICAgIDwvZz4NCjwvc3ZnPg==",
    "name": {
      "en": "User objects and files root folder",
      "de": "Stammordner für Benutzerobjekte und Dateien",
      "ru": "Корневая папка пользовательских объектов и файлов",
      "pt": "Pasta raiz de objetos e arquivos do usuário",
      "nl": "Hoofdmap van objecten en bestanden van gebruikers",
      "fr": "Objets utilisateur et dossier racine des fichiers",
      "it": "Cartella principale di oggetti e file dell'utente",
      "es": "Carpeta raíz de objetos y archivos de usuario",
      "pl": "Folder główny obiektów i plików użytkownika",
      "zh-cn": "用户对象和文件根文件夹"
    },
    "desc": {
      "en": "Here you can upload your files or create your private objects and states",
      "de": "Hier können eigene Dateien hochgeladen oder private Objekte und Zustände erstellt werden",
      "ru": "Здесь вы можете загрузить свои файлы или создать свои личные объекты и состояния",
      "pt": "Aqui você pode enviar seus arquivos ou criar seus objetos e estados particulares",
      "nl": "Hier kunt u uw bestanden uploaden of uw privé-objecten en statussen maken",
      "fr": "Ici, vous pouvez télécharger vos fichiers ou créer vos objets et états privés",
      "it": "Qui puoi caricare i tuoi file o creare oggetti e stati privati",
      "es": "Aquí puede cargar sus archivos o crear sus objetos y estados privados",
      "pl": "Tutaj możesz przesyłać pliki lub tworzyć prywatne obiekty i stany",
      "zh-cn": "在这里您可以上传文件或创建私有对象和状态"
    },
    "type": "meta.user",
    "dontDelete": true
  },
  "acl": {
    "owner": "system.user.admin",
    "ownerGroup": "system.group.administrator",
    "object": 1604
  },
  "from": "system.host.raspberrypi-iobroker.cli",
  "ts": 1633092016078
}

Solution 2

  1. Define a new instanceObject which will be used as default meta storage. eg javascript.<instance> (like vis or admin)
  2. Set the adapter parameter to this.namespace when null.
    {
      "_id": "",
      "type": "meta",
      "common": {
        "name": {
          "en": "user files and images for background",
          "de": "Benutzerdateien und Bilder für den Hintergrund",
          "ru": "пользовательские файлы и изображения для фона",
          "pt": "arquivos e imagens do usuário para plano de fundo",
          "nl": "gebruikersbestanden en afbeeldingen voor achtergrond",
          "fr": "fichiers utilisateur et images pour l'arrière-plan",
          "it": "file utente e immagini per lo sfondo",
          "es": "archivos de usuario e imágenes para el fondo",
          "pl": "pliki użytkownika i obrazy w tle",
          "zh-cn": "背景的用户文件和图像"
        },
        "type": "meta.user"
      },
      "native": {}
    }

Solution 3

Remove that information from documentation 😄

Versions:

Apollon77 commented 2 years ago

Because it never worksed withnull as first param I think option 1 makes sense inl adjusting docs

klein0r commented 2 years ago

I would test it - but every release since 5.7.0 failed and was not published.

https://github.com/ioBroker/ioBroker.javascript/runs/7186405290

I had the same problem with the build process in #1052

klein0r commented 2 years ago

Tested with 5.8.5 ✅