what is the 'use' keyword in javascript?
Asked Answered
S

2

5

I am looking at this specific line of code to understand what it is or to find some documentation about it.

https://github.com/adonisjs/adonis-rally/blob/c7378d2c3984bffba1049f50e771318ea447107c/app/Model/Channel.js

const Lucid = use('Lucid')

I am trying to write a test in adonisjs using mocha and it gives me the following error "ReferenceError: use is not defined"

Something answered 21/12, 2016 at 17:21 Comment(4)
This is not a standard part of Javascript or node.js. In that particular code, it looks like something similar to require() that would be in some library which you do not appear to have available.Wholesale
There is no use keyword. That is a function call - some library defines it. You don't seem to have that library loaded.Kinsey
Look in the package.json to see the installed packagesArmistice
I copied the package json from that project i linked to my project and I still cannot run a test. My model looks exactly the same as the model linked.Something
R
7

The use() function is provided by adonis.js.

use(namespace/alias)

Fetch a binding using it’s namespace or alias.

The adonis-lucid package has an example of how to create a model that looks identical to the code that you've linked in your question. Creating a model docs

Rhetor answered 21/12, 2016 at 17:34 Comment(1)
thanks for your answer. it was very hard finding documentation and vue.js seems to explain a bunch. As for why my test was throwing an error i copied the test.js from the linked repo and it seems to have resolved my issue. my guess is it has to do with starting the server?Something
W
2

To complete the answer. use() function is provided by the IoC Container of AdonisJs (adonis-fold).

This function will try to resolve a binding or a namespace defined in your Adonis configuration file and will then fallback to the default require() function to import a package if it didn't find anything.

Woodrowwoodruff answered 30/12, 2016 at 11:17 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.