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

107,365

Solution 1

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.

Solution 2

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>

Solution 3

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

// remove
import e from 'express';

Solution 4

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

Share:
107,365
Oleksandr H
Author by

Oleksandr H

Updated on July 09, 2022

Comments

  • Oleksandr H
    Oleksandr H almost 2 years

    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

  • kidnan1991
    kidnan1991 over 9 years
    I see this is a correct one. due to the library import order
  • Sander
    Sander over 9 years
    This was the problem for my case as well, thanks! I am using Gulp, so if anyone's using that please check the sequence of loading.
  • zeros-and-ones
    zeros-and-ones about 9 years
    Yeah I renamed the model was collection was referencing, causing this error.
  • ProblemsOfSumit
    ProblemsOfSumit almost 9 years
    god bless StackOverflow. That error message would've never lead me to that conlusion. I'm also a victim of gulps minifying.
  • Guillaume Renoult
    Guillaume Renoult over 8 years
    Thanks, I had the same issue as well. I created the collection model in initialize method instead of defining it as property model of the collection.
  • scones
    scones about 8 years
    Happened even when both were in ready blocks. Had to force a load order.
  • PapaHotelPapa
    PapaHotelPapa almost 8 years
    If anyone needs it, that would make your code: App.Collections.Users = Backbone.Collection.extend({ initialize: function() { user = new App.Models.User(); }, url: 'service' }); ***Only the section relevant to the change shown.
  • Syntaxsizer
    Syntaxsizer about 7 years
    I had similar issue that backbone.min complaining about reading property of undefined i suspected it was a collection issue, it was import order problem lol, Thanks guys