Skip to content

Getting started does not work #262

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

Closed
curquiza opened this issue Mar 21, 2020 · 3 comments · Fixed by #276
Closed

Getting started does not work #262

curquiza opened this issue Mar 21, 2020 · 3 comments · Fixed by #276

Comments

@curquiza
Copy link
Member

curquiza commented Mar 21, 2020

I tried:

$ yarn init
$ yarn add meilisearch

In test.js

const MeiliSearch = require('meilisearch')

const meili = new MeiliSearch({ host: 'http://127.0.0.1:7700' })
const index = await meili.createIndex({ uid: 'indexUID' })

Then:

$ node test.js

I get:

const index = await meili.createIndex({ uid: 'indexUID' })

SyntaxError: await is only valid in async function
    at Module._compile (internal/modules/cjs/loader.js:723:23)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:789:10)
    at Module.load (internal/modules/cjs/loader.js:653:32)
    at tryModuleLoad (internal/modules/cjs/loader.js:593:12)
    at Function.Module._load (internal/modules/cjs/loader.js:585:3)
    at Function.Module.runMain (internal/modules/cjs/loader.js:831:12)
    at startup (internal/bootstrap/node.js:283:19)
    at bootstrapNodeJSCore (internal/bootstrap/node.js:623:3)

(Same error if I try with getIndex)

Can we do a getting started directly working for the JS beginners like me? 😇

Version

meilisearch: v0.9.0
node: v10.18.1

@riccardogiorato
Copy link

How to solve this issue

You are not using an async wrapper. This is an issue related to the correct usage of await.

const MeiliSearch = require("meilisearch");

(async () => {
  try {
    const config = {
      host: "http://127.0.0.1:7700"
    };

    const meili = new MeiliSearch(config);

    await meili.createIndex({ uid: 'yourIndexName' })
  } catch (e) {
    console.log("Meili error: ", e.message);
  }
})();

Library improvement idea just on methods like addDocuments or createIndex:

By wrapping the entire block of your code inside an async block this issue will be solved.

A possible improvement could be handling every method in the library also with a synch version like it's done with Node.js methods like:

  • fs.mkdir(path[, options], callback) "To be used only with async contexts and await/yield"

  • fs.mkdirSync(path[, options]) "This is the synchronous version of fs.mkdir() without use of promise or await"

@curquiza
Copy link
Member Author

Thanks a lot for your help @giorat :) It works like that.

Yes, doing a sync version is in our to-do list, we haven't put any issue about that yet, but I will do it :)

@bidoubiwa
Copy link
Contributor

We are adding a function that is called waitForPendingUpdate and that will await until the action of which ID you gave as parameter has been processed by MeiliSearch.

as you can see here meilisearch/integration-guides#1

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

Successfully merging a pull request may close this issue.

3 participants