The Angular.js resource REST request encountered a TypeError due to a function being undefined

My goal is to update data using a REST call. I have already made a GET request to populate the form, and upon clicking the update button, I intend to initiate a PUT call. However, instead of successfully executing the PUT call, I encounter a Type Error.

Below is the relevant code snippet:

//service.js

someservice.factory('update', function($resource) {
return $resource(connectionString + 'update', {}, {
put: {
  method: "PUT",
  isArray:
    'Content- false,
    headers: {Type': 'application/json',
    'cache-control': 'no-cache',
    'Pragma': 'no-cache',
    'sessionId': gv.sessionId
     }
    }
  })
})

//companyCtrl.js where the update factory is included in controller dependencies

$scope.btnUpdate = function() {
    console.log("asd");
    update.put({}, {
       'data': $scope.data
    }, function(data){
       if(data.success){
          console.log("success");
       }
       else{
          console.log("failure");
       }
    })
}

In my view, I have a button with ng-click="btnUpdate()"

The console log outputs "asd" indicating that everything is properly connected.

One issue that might be contributing to the problem is that I am utilizing ui-router and including the controller within my state configuration as shown below:

.state('admin.company', {
  url: "/company/{_id}",
  templateUrl: "./views/partials/company.html",
  controller: "companyCtrl"

The error stack trace displays the following message:

TypeError: undefined is not a function
at Scope.$scope.btnUpdate (http://localhost:3000/dist/js/app.min.js:1:6680)
at http://localhost:3000/libs/angular/angular.js:10836:21

Answer №1

I was able to resolve the issue by removing a factory dependency, and now all systems are functioning properly.

Answer №2

Does the content of companyCtrl.js end here? It seems that you have not created a function named "companyCtrl" or included it as a controller in your application. The issue can be resolved by updating companyCtrl.js with the following code:

function companyCtrl($scope) {
$scope.btnUpdate = function() {
    console.log("asd");
    update.put({}, {
       'data': $scope.data
     }, function(data){
        if(data.success){
           console.log("success");
        }
        else{
           console.log("failure");
        }
     })
}
}

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Rails gives a response of HTML when angular sends a resource.get request

Having trouble with Rails 4 responding to an angular resource.get request with HTML instead of JSON. The code snippet for the javascript is as follows: $resource("/questions/:id", {id: "@id"}}.get({id: 16}); The server error occurs because it cannot loc ...

Utilizing the forEach method for decision-making

Here is an array called 'array' with values [10, 15, 20] var array = [10, 15, 20]; There is also a variable named N with a value of 20: var N = 20; I need to iterate through the array and check if N is greater than all numbers in the array in ...

Is it possible to create a "text carousel" using HTML, CSS, and JavaScript?

Currently, I am in the process of building a 'text carousel' on my own, without seeking assistance. The progress so far can be viewed here (please stretch it out, as it is not responsive yet). Specifically, I am aiming to replicate the text carou ...

Ways to store information in variables and use it across different blocks in Cypress

Is it feasible to store data in variables and reuse them in other blocks within Cypress.io? For instance, imagine I have a unique name for a device. I aim to retrieve this information and then verify if the title in a new window includes that particular de ...

Example of a Node.js application showcasing the implementation of dependency injection

Angular's method of dependency injection has caught my attention, and I am interested in implementing a similar approach in Node.js. With so many DI container modules available, selecting the right one can be challenging. Can you provide me with real ...

Transferring a value via AJAX that has been modified by a previous AJAX call

In my form, users input values which are used to calculate a result. The calculation is performed through AJAX calls triggered by onchange events on the user-edited fields. This process works as expected. However, there is a hidden field that also gets up ...

Vue.js methods bound as properties on a parent object

There are times when I come across scenarios where it would be convenient to bind methods as an object property rather than a direct Vue method. For instance, instead of: <MyInput :formatter="currencyFormat" :parser="currencyParser& ...

Vue: event triggers malfunctioning and components unresponsive

I am new to Vue.js and I'm attempting to trigger an event from my grand-child component (card) to the child component (hand) and then to the parent component (main): card (emit play event) => hand (listen for play event and emit card-play event) ...

I'm curious to know the meaning of this piece of code in JavaScript. What

Can you explain the purpose of this line in JavaScript? config = config || {} ...

How to separate specific ranges within an array of numbers?

Scenario In a client application, there is a table where users can select specific rows. Each row is identified by an ID which needs to be included in the URL for editing purposes. The process of creating this string occurs every time a row is selected or ...

Angular JS material dialog offering various choices for saving data

I am currently working on creating a dialog with multiple options for saving data, including an option to save and close, another option to simply save, and a cancel option. The goal is for the (save and close) button to save the data and close the dialog, ...

Twitter Bootstrap Grid System Cell Divider

Currently, I have a bootstrap grid with one row and two columns, and I am looking to add a splitter between those columns. Below is how my code looks: <div class="container-fluid"> <div class="row"> <div class="col-md-6"> ...

What is the process for displaying the current bitcoin price on an HTML page using API integration?

I'm looking to develop an application that can display the current Bitcoin price by fetching data from the API at . I want to showcase this information within an h2 element. Any suggestions on how I could achieve this? Thank you in advance. const e ...

Adjust the ng-show attribute in a different controller

I am attempting to toggle the visibility of a div using ng-show. Specifically, I have a navbar that should only be displayed in certain views. I have one controller managing the behavior of this div, and another controller where I want to manipulate the v ...

Convert a Node.js module from synchronous to asynchronous functionality

I recently developed a Node.js module that is designed to handle Handlebars templates. It reads a directory of these templates, compiles them, and then exports an object containing the templates with their names as keys: 'use strict'; var fs ...

What could be causing my function to output unicode replacement characters instead?

As I work on enhancing password security by implementing encryption, my goal is to store the hashes and perform encryption/decryption during signup/login processes. The encryption process works smoothly, converting from utf8 to hex without any issues. Howe ...

What possible reasons could be causing the ng-show directive to malfunction?

This form displays contact information. <div class="row" ng-controller="contactsCtrl"> <form ng-show="addFormShow"> <h3>Add Contact</h3> <!-- Add form --> <div class="row"> <div class="large ...

In JavaScript, the elements within document.forms[0] are distinct from those within document.<formname>

In my attempt to incorporate client-side validation in Struts 2 with the xhtml theme, I have encountered an issue. The JavaScript being generated is unable to validate my code. Upon further investigation, I discovered that Struts is using a specific notati ...

Constructing a regular expression

I've been exploring JavaScript regular expressions and encountering some challenges while trying to build a larger one. Therefore, I have decided to seek help for the entire problem rather than just individual questions. What I am looking for is a re ...

Ad containing a server-side gallery feature (utilizing PHP, JavaScript, and possibly AJAX)

I have a collection of images stored in a folder along with their paths in a database. My goal is to create a photo gallery using these images, complete with previous and next buttons, all without the need for page reloading. Below is the AJAX function I& ...