PouchDB is not defined - javascript

I am trying to instantiate PouchDB in a javascript file. I am importing it with below code :
var imported = document.createElement('script');
imported.type = 'text/javascript';
imported.src = 'pouchdb-6.1.2.js';
document.head.appendChild(imported);
Inside my custom function I am instantiating it as :
var db = new PouchDB('mydb');
This, however, is throwing an error - 'Uncaught ReferenceError: PouchDB is not defined'. Please Help

Did you try this to see if it works:
imported.src='//cdn.jsdelivr.net/pouchdb/6.2.0/pouchdb.min.js'
Also, you can try to add the script tag inside HTML:
<head>
<script src="//cdn.jsdelivr.net/pouchdb/6.2.0/pouchdb.min.js"></script>
</head>

Download the latest PouchDb minified here: https://github.com/pouchdb/pouchdb/releases/download/6.2.0/pouchdb-6.2.0.min.js
Put it in your js or scripts folder and the list it in your index.html file:
<script type="text/javascript" src="js/pouchdb-6.2.0.min.js"></script>

Related

Uncaught Reference Error , method not defined

I am trying to use a method from a javascript module and i dont understand why i get this error :
index.html:5
Uncaught ReferenceError: starts is not defined
at window.onload (index.html:5:11)
I have 2 js files and a html one and all are in the same folder.
connect.js
function start(){
console.log("ok");
}
main.js
import{start} from './connect.js';
export{starts};
function starts(){
start();
console.log("started script");
}
Index.html
<html>
<body>
<script type="text/javascript" >
window.onload=function(ev){
starts();
};
</script>
</body>
<script type="module" src="./connect.js"></script>
<script type="module" src="./main.js"></script>
</html>
If you are trying to use modules from the local filesystem (by directly opening index.html), this is not allowed due to CORS restrictions. See this for more details: javascript modules and CORS
If there are no other project requirements regarding the use of modules, you may use the scripts without type="module" and remove import/export statements from main.js. Otherwise, you would need a server.
You have made a spelling mistake in second line of your main.js
Use export{start} instead of export{starts}; And also change that in HTML file.

function is not defined, when it is

today i was working on a project, and i got this error.
Uncaught ReferenceError: launch is not defined
at HTMLInputElement.onclick (home.html:77)
i don't understand what i did wrong here..
Here is the index.js file:
function launch() {
console.log('test');
}
module.exports.launch = launch;
and home.html:
<script>
let func = require('./index');
let launch = func.launch();
document.getElementById('lanBTN').addEventListener('click', () => {
launch();
});
<input type="button" value="Launch!" id="lanBTN" onclick="launch()">
</script>
Any ideas why this is happening..?
Require is a commonjs module specification, it doesn't work on the browser unless you use some bundler like webpack or browserify to resolve the dependencies between all of you modules and bundles one single js file to include in your html
As #mehdi-belbal mentioned you can not use CommonJS in HTML files expect if when using module bundlers like Webpack.
Besides of that module.exports is useless here, try to link your javascript module in the head of the document. and the declared function after. that will attach to the window object and you can use them both by using window.func() and ‍func()
<head>
<script src="./index.js"></script>
</head>
<body>
...
<script>
func();
</script>
</body>

gzip-js does not work, Uncaught ReferenceError: require is not defined

I would like try gzip-js. and I make a html file to use function of gzip-js
I went https://www.npmjs.com/package/gzip-js, made "npm i gzip-js", as they suggested, the I got some files anf folders,then I wrote an html file(which is in the folder "\node_modules\gzip-js\lib")
<html>
<script src="gzip.js">
var gzip = require('gzip-js');
var out = gzip.zip('Hello world');
console.log(out);
</script>
<body>
</body>
</html>
I hope it will work as the website show, but I was told "Uncaught ReferenceError: require is not defined at gzip.js:4 at gzip.js:280"
in the console of my browser.

How to include a .js file in another .js file when working with QML and QtQuick2? No browser involved

In order to include one .js file in another .js file I tried writing the following in a .js file.
var head= document.getElementsByTagName('head')[0];
var script= document.createElement('script');
script.type= 'text/javascript';
script.src= 'helper.js';
head.appendChild(script);
I got the error: ReferenceError: document is not defined
Then I wrote:
<script type="text/javascript" charset="utf-8">
$(documuent).ready(function () { var editChange = $('td').replaceWith('<td id = "#Html.ValueFor(x => x.name)" >'); });
</script>
on the top of it, but got the error: SyntaxError: Unexpected token <
Problem is that here no browser is involved. This .js file is working along QML and getting compiled with qmake.
What is the syntax for this?
Here is the documentation on "Importing JavaScript Resources in QML" with the specific case of importing in another JS file
If you have a file helper.js and you want to use its functions in another javascript file you can do like that:
.import "helper.js" as Helper
Then you can use one of its functions like this:
Helper.myFunction();

elasticsearch.js: ReferenceError: require is not defined

I'm trying to create a project using elasticsearch.js. I'm following the docs found here.
In the <head> I've loaded:
<script src="js/jquery-1.10.2.js"></script>
<script src="js/elasticsearch.js"></script>
<script src="js/esInit.js"></script>
and in custom.js I have
var elasticsearch = require('elasticsearch');
var client = new elasticsearch.Client({
{Host Here}
});
In FB console I see
ReferenceError: require is not defined
var elasticsearch = require('elasticsearch');
I'm reading that require() is not a function of javascript. Very confused.
It looks like you are loading the version that is supposed to be used with Node.js.
There are browser compatible builds available as well: http://www.elasticsearch.org/guide/en/elasticsearch/client/javascript-api/current/browser-builds.html
Yes require is not a default function in javascript. Instead you need to download that js file from "http://requirejs.org/docs/download.html"

Categories