Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to fix Uncaught TypeError: Cannot read property 'prototype' of undefined?

I have a little problem. I'm using backbone.js. I wrote this code like in example:

<script>
$(document).ready(function () {
    window.App = {
        Views: {},
        Models: {},
        Collections: {}
    }

    App.Collections.Users = Backbone.Collection.extend({
         model: App.Models.User,
         url: 'service'
    });
    App.Models.User = Backbone.Model.extend({});

    App.Views.App = Backbone.View.extend({
         initialize: function() {
             console.log( this.collection.toJSON() );
         }
    });

});
</script>

Than I started server and in browser console type this:

var x =new App.Collections.Users();
x.fetch()

And this follows to error: Uncaught TypeError: Cannot read property 'prototype' of undefined. But data is present in response. Details in picture. How to fix this? Thanks for you answers.enter image description here

enter image description here

like image 666
Oleksandr H Avatar asked Oct 31 '13 09:10

Oleksandr H


4 Answers

I fixed this bug. The problem was that I created Collection and then the Model. Collections use user model, as working unit, but when I defined this Collection, I did not define Model.

So, if you want to avoid this bug, firstly define a Model and only then define the Collection.

like image 142
Oleksandr H Avatar answered Oct 25 '22 09:10

Oleksandr H


Backbone Js has dependency with underscore.js and jQuery, try reordering the resources. This worked for me:

<head>
    <title>Backbone</title>

    <!-- Scripts Load Here-->       
    <script type="text/javascript" src="js/jquery-1.11.0.min.js"></script>
    <script type="text/javascript" src="js/underscore.js"></script>
    <script type="text/javascript" src="js/backbone.js"></script>
</head>
like image 39
rzskhr Avatar answered Oct 25 '22 09:10

rzskhr


In my case , imports were incorrect

import { urlencoded } from "express";
When I removed this statement , it worked for me. Check imports of the files you are working with , wheteher something unnecessary imported

like image 1
vishal dange Avatar answered Oct 25 '22 07:10

vishal dange


In my case, the following imports are incorrect: (react)

// remove
import e from 'express';
like image 1
niek tuytel Avatar answered Oct 25 '22 08:10

niek tuytel