Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Getting TypeError: Client is not a constructor #175

Open
justmedev opened this issue Jul 28, 2021 · 2 comments
Open

Getting TypeError: Client is not a constructor #175

justmedev opened this issue Jul 28, 2021 · 2 comments

Comments

@justmedev
Copy link

Describe the bug
When trying to initialize the client like this:

import Client from "nextcloud-node-client";

const client = new Client();

I get:

file:///C:/Users/ilja/Desktop/Projects/Development/Other/Plugins/steam-cloud-sync/nct.js:3
const client = new Client();
               ^

TypeError: Client is not a constructor
    at file:///C:/Users/ilja/Desktop/Projects/Development/Other/Plugins/steam-cloud-sync/nct.js:3:16
    at ModuleJob.run (node:internal/modules/esm/module_job:183:25)
    at async Loader.import (node:internal/modules/esm/loader:178:24)
    at async Object.loadESM (node:internal/process/esm_loader:68:5)
    at async handleMainPromise (node:internal/modules/run_main:63:12)

To Reproduce

  1. Make a new JS/TS file (with the latest node version)
  2. Import and initialize the client constructor

Expected behavior
Client should get initialized without any trouble

Additional context
Tried it with various TS configs and JS EMS syntax

@hobigo
Copy link
Owner

hobigo commented Jul 29, 2021

I would strongly recommend to switch to TypeScript.
However, import and require with defaults is a mess...

Remove the typings in the example code and use this line to get the client
const Client = require("nextcloud-node-client").Client;

Find the js example example below.
Regards Holger


// tslint:disable:no-console
// get files recursively

const Client = require("nextcloud-node-client").Client;

(async () => {
    const client = new Client();

    const sourceFolder = await client.getFolder("/Borstenson/Company Information");
    if (!sourceFolder) {
        console.log("source folder not found");
        process.exit(1);
    }

    // only pdfs and jpg should be listed
    const fileFilterFunction = (file) => {
        if (file.mime === "application/pdf" || file.mime === "image/jpeg") {
            return file;
        }
        return null;
    }

    const options = {
        sourceFolder,
        filterFile: fileFilterFunction,
    };

    const command = new GetFilesRecursivelyCommand(client, options);
    // get files asynchronously (will not throw exceptions!)
    command.execute();

    // check the processing status as long as the command is running
    while (command.isFinished() !== true) {
        // wait one second
        await (async () => { return new Promise(resolve => setTimeout(resolve, 1000)) })();
        console.log(command.getPercentCompleted() + "%");
    }

    // use the result to do the needful
    const commandResult = command.getResultMetaData();

    if (command.getStatus() === CommandStatus.success) {
        console.log(commandResult.messages);
        for (const file of command.getFiles()) {
            console.log(file.name);
        }
    } else {
        console.log(commandResult.errors);
    }

})();



@justmedev
Copy link
Author

Ok. So I've tried your example with es modules. It works, but there's a weird thing I had to do to get it working.
I had to add .default to the Client class when constructing it.
What my script looks like now:

import Client from 'nextcloud-node-client';
console.log(Client.default);

(async () => {
  const client = new Client.default();
  console.log('Should work without any errors`);
})();

You maybe should put that in your documentation.
But thanks for your help anyways. If you find a better way to do it with esm: tell me!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants