Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

TypeError: Cannot read property 'jquery' of undefined

I'm getting the following error in Chrome console,

TypeError: Cannot read property 'jquery' of undefined

Here is my javascript function,

<script type="text/javascript">
    var formApp = angular.module('formApp',[]);

    function formController($scope,$http){
        $scope.formData = {};

        $scope.processForm = function(){
            $http({
                method : 'POST',
                url : 'http://localhost:8080/ABCAPI/v1/image/front',
                data : $.param($scope.formData.imageFile),
                headers : {'Content-Type': "multipart/form-data" , 'Auth-Token' : "X"}
            }).success(function(data){
                console.log("");

                if (!data.success) {
                    // if not successful, bind errors to error variables

                } else {
                    // if successful, bind success message to message
                    $scope.message = data.message;
                }
            })
        };
    }

</script>

What does above error means and where did I go wrong?

like image 895
helloworld Avatar asked Apr 18 '14 08:04

helloworld


People also ask

How do you fix TypeError Cannot read properties of undefined?

To solve the "Cannot read properties of undefined" error, use the optional chaining operator to check if the variable is not nullish before accessing it, e.g. person?. name . If the variable is undefined or null , the operator short-circuits instead of throwing an error.

What is TypeError Cannot read property of undefined?

The “cannot read property of undefined” error occurs when you attempt to access a property of a variable that is undefined . For example: const obj = undefined; // TypeError: Cannot read properties of undefined (reading 'prop') console. log(obj.

Can not read the property of undefined Reading 0?

The "Cannot read Property '0' of undefined" error occurs when accessing an undefined value at index 0 . To solve the error, initialize the variable to the correct data type, e.g. array or string, before accessing the index.

What does Cannot read properties of null mean?

The "Cannot read property 'click' of null" error occurs when trying to call the click method on a null value. To solve the error, run the JS script after the DOM elements are available and make sure you only call the method on valid DOM elements.


1 Answers

$scope.formData.imageFile seems to evaluate to undefined.

$.param(undefined) //throws Cannot read property 'jquery' of undefined

Do a console.log($scope.formData) right before the $http() call. The $scope.formData object most likely does not contain an imageFile property, or imageFile is null/undefined.

Note that $.param takes a plain object or array as the first argument, providing another type of value (or no value) falls under undefined behavior.


Also, if I'm reading your code correctly, you're trying to upload an image file through $http. It won't work that way, you need XHR2's FormData API. See this answer for a simple implementation.

Angular does not support ng-model on <input type="file">, which is the reason for the former error.

like image 128
Fabrício Matté Avatar answered Sep 19 '22 15:09

Fabrício Matté