When I try to integrate the script type="module"... I get a reference error. My functions inside the "module" script are not found.
Im trying to integrate a printer into an existing webpage, the printer functionality is defined in the file "bpac.js". Its a javascript module using exports.
I have tried using this code in a sample website an everything worked fine. When I use my code in the existing file I get the error. When I dont use the "module" tag everything works fine, but then I cant use the functionality provided by "bpac.js". I have a simple button that when pressed, should execute some testfunction.
<script type = "module">
import * as bpac from './bpac.js';
window.testfunc = async function testfunc() {
const doc = bpac.IDocument;
const pName = await doc.GetPrinterName();
console.log(pName);
}
</script>
...
<button type="button" id="testen" onclick="testfunc()">test</button><br>
...
When I press the Button I get the Message: Uncaught ReferenceError: testfunc is not defined
at HTMLButtonElement.onclick ((index):1)
You are declaring function in wrong way. Use this.
window.testfunc = async function() {
const doc = bpac.IDocument;
const pName = await doc.GetPrinterName();
console.log(pName);
}
Why don't you use addEventListener to add as many listeners as you want?
document.getElementById("testen").addEventListener('click',function () {
console.log("Hello");
// Add your logic inside this function
});
Related
I have searching for a solution for this error and nothing works so far. I want to export a class from another file so that it can still be used by the main file.
This is the error I get when I run the program.
E:\VSC\block.js:5
let block1 = new Blockchain();
^
TypeError: Blockchain is not a constructor
Here is my code.
const Blockchain = require('./blockchain.js');
const Transaction = require('./blockchain.js');
let block1 = new Blockchain();
I suspect this is some sort of syntax error but I cant see anything wrong with the code.
Thanks.
You didn't include the source code for blockchain.js so we don't know how things are exported from there. My guess is both Blockchain and Transaction are exported and I'm assuming none of them are default export. If this is the case, you can try this:
const { Blockchain, Transaction } = require('./blockchain.js');
let block1 = new Blockchain();
It could simply be that the module.exports was not placed outside the closing bracket in your
I have created a code that it actually works, but I call a library that has an error, and I would like to know if it is possible to avoid that specific line of code. I will try to explain the case as well as possible:
Error
Uncaught TypeError: fs.openSync is not a function
Previous code
function synthesizeToAudioFile(authorizationToken, message) {
// replace with your own subscription key,
// service region (e.g., "westus"), and
// the name of the file you save the synthesized audio.
var serviceRegion = "westus"; // e.g., "westus"
var filename = "./audiocue.wav";
//Use token.Otherwise use the provided subscription key
var audioConfig, speechConfig;
audioConfig = SpeechSDK.AudioConfig.fromAudioFileOutput(filename);
speechConfig = SpeechSDK.SpeechConfig.fromAuthorizationToken(authorizationToken, serviceRegion);
// create the speech synthesizer.
var synthesizer = new SpeechSDK.SpeechSynthesizer(speechConfig, audioConfig);
// start the synthesizer and wait for a result.
synthesizer.speakTextAsync(message,
function (result) {
if (result.reason === SpeechSDK.ResultReason.SynthesizingAudioCompleted) {
console.log("synthesis finished.");
} else {
console.error("Speech synthesis canceled, " + result.errorDetails +
"\nDid you update the subscription info?");
}
synthesizer.close();
synthesizer = undefined;
},
function (err) {
console.trace("err - " + err);
synthesizer.close();
synthesizer = undefined;
});
console.log("Now synthesizing to: " + filename);
}
I created a method, which later I have replicated in my current code. The difference was that I was using Browserify in order to import a library from a script of a HTML file:
<script type="text/javascript" src="js/dist/sub_mqtt.js"></script>
This file had my method, and the whole library, which made it crazy unreadable, and therefore I started using ScriptJS to import it. The problem is that, using browserify I was able to remove the line of code that it was failing using fs.openSync(and I do not even need), but by importing it with ScriptJS I do not have access to the source code.
I assume that what is missing is that I am not importing the library fs, which is being used by the library that I am importing with ScriptJS before importing that one, but how could I do it? I have tried:
<script src="../text-to-speech/node_modules/fs.realpath/index.js"></script>
, or
<script type="text/javascript" src="../text-to-speech/node_modules/fs.realpath/index.js"></script>
and also wrapping the content of synthesizeToAudioFile() with
require(["node_modules/fs.realpath/index.js"], function (fs) { });
but I get the following error:
Uncaught ReferenceError: module is not defined
at index.js:1
After researching about this question I found out the next statement:
The fs package on npm was empty and didn't do anything, however many
packages mistakenly depended on it. npm, Inc. has taken ownership of
it.
It's also a built-in Node module. If you've depended on fs, you can
safely remove it from your package dependencies.
therefore what I have done is to access to the file that I was requiring with ScriptJS
require(["../text-to-speech/microsoft.cognitiveservices.speech.sdk.bundle.js"]
and directly remove that line on it. Note that, at least in my case, clearing the cache of the browser was needed.
Hi working on a project at the moment and currently getting it to call the api, great! however I'm looking at refactoring my test files as there will be quite a few so initially this is how I've done it.
var chakram = require('chakram');
expect = chakram.expect;
var baseFixture = require('../../test/fixtures/');
it("Should return the matching test file", function () {
var response = chakram.get("http://testurl");
return expect(response).to.have.json(baseFixture + 'testfile.json')
});
However with the code above I'm getting this error.
Error: Cannot find module '../../../../test/fixtures'
If I put the json file into the variable at declared at the top with a call to base fixture within the function the test will pass.
Where am I going wrong?
Thanks in advance.
Imagine the following dataset:
{"FakeEconomy":{
"2016-04-05":5651694,
"2016-04-06":5513759,
"2016-04-07":5410169,
"2016-04-08":5094142,
"2016-04-09":4768829,
"2016-04-10":5101458,
"2016-04-11":5776419,
"2016-04-12":5692041,
"2016-04-13":5568383,
"2016-04-14":5555027,
"2016-04-15":5116844,
"2016-04-16":4653882,
"2016-04-17":5112466,
"2016-04-18":5764588
}}
When I pass it through the jQuery each function, it works fine, but then it throws a random:
Uncaught TypeError: Cannot read 'visits' of undefined
The following is the jQuery code:
console.log(dataset['visits']); // Prints the data above
$.each(dataset['visits'], function(index,value) {
pageName = index;
$.each(dataset['visits'][index], function(index,value) {
timeline.push(index);
visits_data.push(parseInt(value));
console.log(timeline);
})
});
What's causing the error?
Found out that the reason the program is raising an error is because I placed the jQuery ready function, which within it includes the function I wrote above, in the JavaScript file versus the HTML file.
In essence, this:
$(document).ready(function(){
var collected_results = graph_data(dataset); // Has the each function
var visitsChart = new Chart(collected_results[0], collected_results[1]);
var devicesChart = new Chart(collected_results[2], collected_results[3]);
var osChart = new Chart(collected_results[4], collected_results[5]);
})
was in the JavaScript file, and the JavaScript file loads faster than the HTML file.
Because the JS file loads faster than the HTML file, it raises the error because it cannot read the objects.
I have a module "./lib/common.js", like this:
function foo(text){
console.log(text);
}
function boo(text){
console.log(text);
}
module.exports=foo;
module.exports=boo;
I'm trying to include these functions in another js file using browserify :
var common =require('./lib/common.js');
$(document).ready(function() {
common.foo('hi');
});
Browserify creates the bundle,but on the browser I get
Uncaught TypeError: common.foo is not a function
Ok this was very stupid, I was overwriting my module.exports with the last row module.exports=boo;
With this change it works:
module.export.foo=foo;
module.export.boo=boo;