Having trouble with updating AngularJS?

I am facing an issue while updating the guitar object in my AngularJS application. The operation is performed using the updateGuitar controller with ngMock backend.

After updating the guitar object, the put request is processed by the carService. However, I encounter a problem in accessing the updated object in my carView controller. So, when I navigate to location.path(/car/view/id, I cannot retrieve the updated object.

Below is the code for my guitarView controller:

window.app.controller('guitarViewCtrl', ['$scope', '$routeParams', '$location', 
'GuitarService', function ($scope, $routeParams, $location, GuitarService) {
 'use strict';

     $scope.guitarId = $routeParams.guitarId;
     initGuitar($scope.guitarId);

     function initGuitar(guitarId) {
       GuitarService.showDetails(guitarId).then(function success(guitar) {  
           $scope.guitar = guitar;            
       }, function error(response) {
       });
     }
}]);

Regarding My GuitarService: I have been advised to populate the Guitars within this service (currently done in mock.js), but I am unsure of how to go about it.

window.app.service('GuitarService', ['HTTPService', '$http', function (HTTPService, $q, $http) {
'use strict';

    this.showDetails = function (opleidingsprofielId){

      // HTTP service returns correct urls
      return HTTPService.get('/opleidingsprofiel/view/' + opleidingsprofielId);

     this.put = function (opformUpdate, opleidingsprofielId) {
         var deferred = $q.defer();
          $http.put('/#/opleidingsprofiel/:opleidingsprofielId/update', opformUpdate)
             .then(function resolve(response){
                 deferred.resolve(response.data);
              }, function reject(response){
                 deferred.reject(response);
              });
            return deferred.promise;
           };
     }]);

Answer №1

There are a few issues that stand out immediately when looking at this code snippet. Firstly, the `this.put` line is never executed as intended because the closing brace `}` to finish the `showDetails` function is missing. This results in the return statement occurring before reaching the `this.put` line. Additionally, in the variable injection with the array, `$q` is missing causing potential errors related to `$http`. It is important to ensure that the code being run matches the code provided, or ideally showcase it in a functional plnkr or codepen example.

Template


this is some made-up code
because sometimes platforms can be frustrating

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

Request the generic password prior to revealing the concealed div

I want to implement a feature where a hidden div is shown once a user enters a password. The password doesn't need to be stored in a database, it can be something simple like 'testpass' for now. Currently, I have written some code using Java ...

Customize Material UI (MUI) Autocomplete with preset initial selections

My goal is to develop a unique MUI Autocomplete feature that showcases a series of numbers from 1 to 50. Upon user selection, the component should initially only show numbers 1, 6, 10, 12, and 24. If the user inputs '1', it should then display al ...

The unhandled type error rises with each scroll I make

Having been diligently working on my current website project, I encountered a puzzling issue. Upon writing code to implement smooth scrolling throughout the site, I found myself facing a persistent error that continues to escalate with each scroll up or do ...

Observing the Transformation When Employing *ngIf or *ngSwitchCase in Angular 2

Can someone lend a hand? I've run into an issue where my custom JavaScript function is not working after using *ngIf or *ngSwitchCase to change the view. Any suggestions on how to resolve this would be greatly appreciated. ...

What is the best way to retrieve the response from an Observable/http/async call in Angular?

My service returns an observable that makes an http request to my server and receives data. However, I am consistently getting undefined when trying to use this data. What could be causing this issue? Service: @Injectable() export class EventService { ...

Monitor the completion status of all Ajax requests using only JavaScript

I am aware of the ajaxStop method in jQuery. $(document).ajaxStop(function() { //Do something }); If jQuery is not available, is there a way to achieve this with pure JavaScript instead? If so, could you please provide an example? Thanks ...

Looping the jQuery Ajax success function

Utilizing Ajax to input an array of data into a database. At the moment, when clicking on "#bookingbutton," it triggers a return block of HTML containing the ".select-room-button" button. I have incorporated the code for ".select-room-button" within the ...

Executing Parent function in Reactjs (Triggering Drawer when menu item is clicked using Material-ui)

I'm having some trouble trying to activate the drawer when a user clicks on a menu item. Unfortunately, my current solution is not working as expected. Can anyone provide assistance? Parent import React, { Component } from 'react'; // Impo ...

How well are DOM Mutation Observers supported across different web browsers?

I attempted to search for a solution by Googling, but was unsuccessful in finding an answer. Does anyone know if there is a compatibility matrix available for this feature that works across all browsers? In case someone is interested in the answer, here ...

"Discover the latest feature in the new Angular router: automatic scrolling functionality

The old ui-router and 1.4 angular router used to support autoscroll="true" to automatically scroll the page to the top when navigating to another route. Is there a way to achieve this with the latest new angular router? It seems like ng-outlet does not ha ...

Ending the jQuery Modal box

Struggling to create a simple modal on my own, and I'm facing some difficulties (probably because I'm more of an expert in jQuery - but let's not dwell on that too much). This is the HTML markup I have: <div id="modal-boxes"> < ...

Issue with BackboneJS TypeError

I attempted to use the example below, but I encountered an error stating "TypeError: _.has is not a function". Example: I have experimented with different versions of jQuery and Backbone (uncompressed), yet the same error persists. Can anyone offer assis ...

The consequences of jQuery Ajax Memory Leaks

After reading through several other posts on the topic, I have noticed a memory leak issue when making repeated ajax calls with jQuery (every 30 seconds in my case). Switching from using $get to $post helped reduce the size of the leak, but it still occurs ...

Guarantee the successful execution of a server-side function using my client-side function

I am currently in the process of creating a website that utilizes Javascript and Asp.net. My code contains numerous functions on the client side, within my html code, while my server side functions are called using a webservice. How can I ensure that my c ...

Navigating websites: Clicking buttons and analyzing content

When looking to navigate a website's directory by utilizing buttons on the page or calling the associated functions directly, what language or environment is most suitable for this task? After experimenting with Python, Java, Selenium, and JavaScript, ...

Issue with unbinding events in Angular directive persists despite efforts to completely unbind

Our directive features a popup that loads a template with a directive to capture all clicks in the document. When a click is detected, it sends an event to close the popup and unbinds the event on the $document. This directive effectively captures docume ...

Dynamically obtaining the screen width in JSP using a variable

Could someone provide assistance on how to resize an image using an img src="blah.jpg?width=x" so that my page can be displayed at various sizes? I just need x (width) as a JSP variable. Update 2021: It's been 9 years since this question wa ...

Tips for customizing the border of an outlined TextField in MUI

Below is the current configuration of a TextField component: const styles = { resize: { fontSize: '50px', } } const textField = (props) => { const { classes } = props; return ( <TextField valu ...

Tips for updating the value of a key in a JavaScript object when the TextField tag's value changes

Here is the schema I am using for my model: const workoutSchema = mongoose.Schema({ workouts: [ { workoutName: String, sets: Number, reps: Number, weight: Number, }, ], }); Below is the postData referenced in the text f ...

How to Implement Snap-Enabled Dragging in a Container Using jQuery UI

Issue Description (Fiddle): I am using the jQuery UI's .draggable() function to make the red element snap inside the blue container. However, it is snapping to one edge only instead of completely fitting inside (both edges). It requires further dragg ...