Uncaught ReferenceError: describe is not defined cors - javascript

I am building a phonegap app which has a cors.js with the following code:
describe('cors', function () {
it('passes control to next middleware', function (done) {
// arrange
var req, res, next;
req = fakeRequest();
res = fakeResponse();
next = function () {
done();
};
The error I am getting is:
Uncaught ReferenceError: describe is not defined
Where do I set the reference to describe?

When I had this problem, i solved it by switching the order of the libraries.
<script src="/bower_components/angular/angular.min.js"></script>
...
<script src="/bower_components/jasmine/lib/jasmine-core/jasmine.js"></script>
<script src="/bower_components/jasmine/lib/jasmine-core/jasmine-html.js"></script>
<script src="/bower_components/jasmine/lib/jasmine-core/boot.js"></script>
<script src="/bower_components/angular-mocks/angular-mocks.js"></script>
... then the tests e.g.
<script src="/js/lib/sync/test/local-test.js"></script>
Also I import all of them at the end of the <body>.

Related

an't access lexical declaration 'myInput' before initialization

In my html file I've got script imports:
<head>
<meta charset="UTF-8">
<title>Title</title>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
<script type="module" src="myInput.js"></script>
<script type="text/javascript" src="example.js"></script>
</head>
myInput.js has namespace and class defined:
let myinput = module.exports = {};
myinput.MYInput = class {
constructor(params) {...}
myinput.init = (params) => {
return new myinput.MYInput(params)
}
and I'd like to use it in my example.js file:
$(window).ready(function() {
console.log( "ready!" );
let this_input = myinput.MYInput.init({...})
Unfortunately the result is:
Uncaught ReferenceError: module is not defined
<anonymous> http://localhost:63342/myinput/myinput.js:1
myinput.js:1:15
GEThttp://localhost:63342/favicon.ico
[HTTP/1.1 404 Not Found 4ms]
ready! example.js:2:13
jQuery.Deferred exception: can't access lexical declaration 'myinput' before initialization #http://localhost:63342/myinput/example.js:6:19
e#https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js:2:30005
l/</t<#https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js:2:30307
setTimeout handler*l/<#https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js:2:30516
c#https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js:2:28294
fireWith#https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js:2:29039
fire#https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js:2:29075
c#https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js:2:28294
fireWith#https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js:2:29039
ready#https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js:2:32012
B#https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js:2:31791
EventListener.handleEvent*#https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js:2:32160
#https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js:2:220
#https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js:2:225
undefined jquery.min.js:2:31560
Uncaught ReferenceError: can't access lexical declaration 'myinput' before initialization
<anonymous> http://localhost:63342/myinput/example.js:6
jQuery 13
I've tried importing that file in different ways including required and from... in example.js but nothing was successful. I do suspect there are couple of problems here but I cannot pinpoint them. Could you help?

TypeError: EventList is not a constructor in one place, in another code works

Strange situation, I have django (1.8) app with feature in js. In one place everything is OK but in another I get an error:
Uncaught TypeError: EventList is not a constructor
Code in js file:
var EventList=function(){};
EventList.prototype.init=function(){this.$eventslist=$(".event-list")
...
When I try call with eventList = EventList(); I get message: Uncaught TypeError: EventList is not a function
Code in HTML file, in js code block:
<script src="{% static 'js/event_list.min.js' %}" type="text/javascript"></script>
<script type="text/javascript">
eventList = new EventList();
eventList.init();
</script>
have you tried
<script type="text/javascript">
document.addEventListener('DOMContentLoaded', function() { var
var eventList = new EventList();
eventList.init();
});
</script>
Maybe you need to wait for the document is ready or you need to "var" your eventList

Service is not defined

I'm having an error while using AngularJS, I can't import a Service from one module to another. I have a Service called MenuDataService in module Data, that I want to use in module MenuApp, and when I try to do it, it gives an error with the following link https://docs.angularjs.org/error/$injector/unpr?p0=MenuDataServiceProvider%20%3C-%20MenuDataService%20%3C-%20CategoriesController.
src/data-module/data.module.js:
angular.module('Data', []);
src/data-module/menudata.service.js:
angular.module('Data')
.constant('CATEGORIES_URI', 'some_uri')
.service('MenuDataService ', MenuDataService);
MenuDataService.$inject = ['$http', 'CATEGORIES_URI'];
function MenuDataService($http, CATEGORIES_URI) {
var service = this;
service.getAllCategories = function () {
return httpRequest(CATEGORIES_URI);
};
};
src/menuapp-module/menuapp.module.js:
angular.module('MenuApp', ['Data']);
src/menuapp-module/categories.controller.js:
angular.module('MenuApp')
.controller('CategoriesController', CategoriesController);
CategoriesController.$inject = ['MenuDataService'];
function CategoriesController(MenuDataService) {
console.log('CATEGORIES CONTROLLER');
};
index.html:
<script type="text/javascript" src="./lib/angular.min.js"></script>
<script type="text/javascript" src="./src/data-module/data.module.js"></script>
<script type="text/javascript" src="./src/data-module/menudata.service.js"></script>
<script type="text/javascript" src="./src/menuapp-module/menuapp.module.js"></script>
<script type="text/javascript" src="./src/menuapp-module/categories.controller.js"></script>
Any help would be great since I don't know what I'm doing wrong...
Thank you very much!
There is an extra space in the service name that you define.
.service('MenuDataService ', MenuDataService);
^

How to pass an object to an module in require.js?

I have these js files:
main.js:
requirejs(['app']);
app.js:
define(['messages'], function (messages) {
alert(messages.getHello());
});
messages.js:
define(['global'],function () {
var privateFn = global.getObj()
return {
getHello: function () {
if(privateFn.hello == "test!")
return 'Hello World';
}
};
});
global.js:
define(function () {
var stateObj = {hello:"test!"};
return {
getObj: function () { return stateObj; }
};
});
and index.html as:
<!DOCTYPE html>
<html>
<head>
<!-- Include the RequireJS library. We supply the "data-main" attribute to let
RequireJS know which file it should load. This file (scripts/main.js) can
be seen as the entry point (main) of the application. -->
<script data-main="scripts/main" src="lib/require.js"></script>
</head>
<body>
<h1>Example 2: load module using explicit dependency syntax</h1>
</body>
</html>
However when I open index.html, I get the below error in console:
Uncaught ReferenceError: global is not defined messages.js
where I'm making mistake?
You just need to set global as an argument to messages.js' function. requirejs will pass it in for you.
messages.js:
define(['global'],function (global) {
var privateFn = global.getObj()
return {
getHello: function () {
if(privateFn.hello == "test!")
return 'Hello World';
}
};
});
This has the neat side effect that it is impossible to reference a module without declaring it as a dependency.

"Uncaught TypeError: undefined is not a function" - Beginner Backbone.js Application

I'm setting up a pretty simple app with backbone, and I'm getting an error.
Uncaught TypeError: undefined is not a function example_app.js:7
ExampleApp.initialize example_app.js:7
(anonymous function)
This is where the error is showing up in Chrome Inspector (init file - example_app.js):
var ExampleApp = {
Models: {},
Collections: {},
Views: {},
Routers: {},
initialize: function() {
var tasks = new ExampleApp.Collections.Tasks(data.tasks);
new ExampleApp.Routers.Tasks({ tasks: tasks });
Backbone.history.start();
}
};
Here's my tasks index.haml file
- content_for :javascript do
- javascript_tag do
ExampleApp.initialize({ tasks: #{raw #tasks.to_json} });
= yield :javascript
models / task.js
var Task = Backbone.Model.extend({});
collections / tasks.js
var Tasks = Backbone.Collection.extend({
model: Task,
url: '/tasks'
});
routers / tasks.js
ExampleApp.Routers.Tasks = Backbone.Router.extend({
routes: {
"": "index"
},
index: function() {
alert('test');
// var view = new ExampleApp.Views.TaskIndex({ collection: ExampleApp.tasks });
// $('body').html(view.render().$el);
}
});
And here's proof that I'm calling all of the files (I think):
<script src="/assets/jquery.js?body=1" type="text/javascript"></script>
<script src="/assets/jquery_ujs.js?body=1" type="text/javascript"></script>
<script src="/assets/jquery-ui.js?body=1" type="text/javascript"></script>
<script src="/assets/underscore.js?body=1" type="text/javascript"></script>
<script src="/assets/backbone.js?body=1" type="text/javascript"></script>
<script src="/assets/backbone-support/support.js?body=1" type="text/javascript"></script>
<script src="/assets/backbone-support/composite_view.js?body=1" type="text/javascript"></script>
<script src="/assets/backbone-support/swapping_router.js?body=1" type="text/javascript"></script>
<script src="/assets/backbone-support.js?body=1" type="text/javascript"></script>
<script src="/assets/example_app.js?body=1" type="text/javascript"></script>
<script src="/assets/easing.js?body=1" type="text/javascript"></script>
<script src="/assets/modernizr.js?body=1" type="text/javascript"></script>
<script src="/assets/models/task.js?body=1" type="text/javascript"></script>
<script src="/assets/collections/tasks.js?body=1" type="text/javascript"></script>
<script src="/assets/views/task_view.js?body=1" type="text/javascript"></script>
<script src="/assets/views/tasks.js?body=1" type="text/javascript"></script>
<script src="/assets/views/tasks_index.js?body=1" type="text/javascript"></script>
<script src="/assets/routers/tasks.js?body=1" type="text/javascript"></script>
<script src="/assets/tasks/index.js?body=1" type="text/javascript"></script>
<script src="/assets/tasks/task.js?body=1" type="text/javascript"></script>
<script src="/assets/application.js?body=1" type="text/javascript"></script>
Any ideas would be great. Thanks!
Uncaught TypeError: undefined is not a function example_app.js:7
This error message tells the whole story. On this line, you are trying to execute a function. However, whatever is being executed is not a function! Instead, it's undefined.
So what's on example_app.js line 7? Looks like this:
var tasks = new ExampleApp.Collections.Tasks(data.tasks);
There is only one function being run on that line. We found the problem! ExampleApp.Collections.Tasks is undefined.
So lets look at where that is declared:
var Tasks = Backbone.Collection.extend({
model: Task,
url: '/tasks'
});
If that's all the code for this collection, then the root cause is right here. You assign the constructor to global variable, called Tasks. But you never add it to the ExampleApp.Collections object, a place you later expect it to be.
Change that to this, and I bet you'd be good.
ExampleApp.Collections.Tasks = Backbone.Collection.extend({
model: Task,
url: '/tasks'
});
See how important the proper names and line numbers are in figuring this out? Never ever regard errors as binary (it works or it doesn't). Instead read the error, in most cases the error message itself gives you the critical clues you need to trace through to find the real issue.
In Javascript, when you execute a function, it's evaluated like:
expression.that('returns').aFunctionObject(); // js
execute -> expression.that('returns').aFunctionObject // what the JS engine does
That expression can be complex. So when you see undefined is not a function it means that expression did not return a function object. So you have to figure out why what you are trying to execute isn't a function.
And in this case, it was because you didn't put something where you thought you did.
I have occurred the same error look following example-
async.waterfall([function(waterCB) {
waterCB(null);
}, function(**inputArray**, waterCB) {
waterCB(null);
}], function(waterErr, waterResult) {
console.log('Done');
});
In the above waterfall function, I am accepting inputArray parameter in waterfall 2nd function. But this inputArray not passed in waterfall 1st function in waterCB.
Cheak your function parameters Below are a correct example.
async.waterfall([function(waterCB) {
waterCB(null, **inputArray**);
}, function(**inputArray**, waterCB) {
waterCB(null);
}], function(waterErr, waterResult) {
console.log('Done');
});
Thanks
[Joke mode on]
You can fix this by adding this:
https://github.com/donavon/undefined-is-a-function
import { undefined } from 'undefined-is-a-function';
// Fixed! undefined is now a function.
[joke mode off]
If in MongoDB with async-await , check if you have a missing await before the mongo call.
const [assistantError, assistant] = await assistantDb.findOneAssistant({
_id: assistantId,
});
Here if the await was missing, later when you use assistant, this issue may occur.

Categories