Define URL parameters within the ngResource framework

My current setup involves utilizing ngResource in my service to retrieve comments for specific news posts within my web application.

However, I am facing an issue where the query for comments for a particular news post,

article.comments = commentService.query()
, is being sent to /api/news/comments instead of /api/news/:id/comments. How do I specify the :id parameter so that the request is directed to the correct URL (/api/news/:id/comments)?

Here is the configuration for the commentService:

   .factory('commentService', function($resource){
        return $resource('/api/news/:id/comments/:comment', {id: '@news', comment: '@comment'}, {
            update: {
                method: 'PUT'
            }
        }, {
            stripTrailingSlashes: false
        }
    )})

Method for fetching comments on ng-click:

$scope.getComments = function(article) {
    article.comments = commentService.query()
}

Answer №1

This was my approach to finding the solution

$scope.fetchComments = function(post) {
    return commentService.retrieve({postId: post._id})
}

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

Arrange divs on the screen in a specific formation

I am exploring ways to position specific divs in the middle of the screen to create a pearl necklace-like shape. The desired shape is as follows: 0 0 0 0 0 0 0 0 0 0 My aim is to achieve this using jQuery on a mobile ...

Issue: $injector:unpr Unrecognized Provider: itemslistProvider <-

I've spent several days debugging the code, but I can't seem to find a solution. I've gone through the AngularJS documentation and numerous Stack Overflow questions related to the error, yet I'm still unable to identify what's caus ...

Using the scrollIntoView() method in VUE.js to center an li element within a component

One of the components I'm working with has multiple list items, and I want to achieve a functionality where clicking on any item will center it inside the component, making it visible in the center of the view. <card-maintenance v-for="m ...

Is it possible to send an ajax request to a user control file with the extension .ascx?

I am trying to interact with a user control on my page through ajax. Is it possible to make an ajax request directly to the user control (.ascx) instead of .aspx or .ashx files? ...

$routeProvider is encountering difficulties in retrieving the HTML template

I am facing an issue despite adding both angular.min.js and angular-route.min.js files. Here is the code in app.js: var myApp = angular.module('myApp',[ 'ngRoute', 'artistControllers' ]); myApp.config(['$routeProvider& ...

Axios removes the async functionality

Is there a way to achieve the same functionality without using the async function? async EnvioLogin() { const response = await axios.post("api/auth/login", { email: this.email, password: this.password, }); localStorage.setItem(" ...

Steps for adding a JSON object to an unordered list

After receiving data from a web server, I created a JSON object using the code obj = JSON.parse(data). This object contains information about dinosaurs such as their name, group, diet, and period. This is what the JSON object looks like: [{"name":"Stauri ...

How to refresh a specific component or page in Angular without causing the entire page to reload

Is there a way to make the selected file visible without having to reload the entire page? I want to find a cleaner method for displaying the uploaded document. public onFileSelected(event): void { console.log(this.fileId) const file = event.targe ...

Is there a way to cross-reference a city obtained from geolocation with the cities stored in my database, and if a match is found, send the corresponding ID

Here's the script I'm currently working with: <script type="text/javascript"> search = new Vue({ el: '#offers', data: { data: [], authType: '{{uid}}', key : '1', wi ...

Guide on effectively clearing the context and canvas in three.js

We are currently developing an application that is also compatible with use on iPads, utilizing three.js version r100. Within our application, we have a "main" component and various "popups", each equipped with its own canvas, scene, and renderer. The "ma ...

Execute a personalized function when an array is updated or created in JavaScript

I have experience in adding custom properties to objects. For example, if I have a method called foo, const foo = () => { console.log('custom method'); } I can add the foo method to the Array prototype and use it with array variables by Arra ...

JSPM encountered an issue with installation from GitHub (404 error), but it is able to successfully install packages from npm

I am encountering a frustrating issue with my Package.json file for a GitHub repository in my organization. Attempting to pull it in via jspm is causing errors. { "name": "tf-modernizr", "version": "1.0.0", "description": "", "main": "modernizr.js ...

Tips for preserving selection state in a multi-page results display?

In the process of developing a web application, I am working on implementing a way to save selection states. Specifically, I am building a query interface that interacts with a MongoDB backend. The search results are displayed in a grid format with check ...

What is the most effective approach for annotating TypeScript abstract classes that are dynamically loaded?

I am in the process of developing a library that allows for the integration of external implementations, and I am exploring the optimal approach to defining types for these implementations. Illustration abstract class Creature { public abstract makeN ...

Issue with SideNav function in MaterializeCss following recent update

After updating the css/js files of the Materializecss design in my project from v0.97.5 to v0.97.8, I noticed that my SideNav isn't functioning correctly anymore. When I try to click on the menu, it slides out but the dark overlay covers the entire sc ...

Error: Unable to render followers list due to incorrect data type

I have embarked on creating a unique Github user card generator that retrieves user data and their followers' information from the GitHub API, then displays them on a personalized user card. The follower's data received is in the form of an array ...

The conversion to ObjectId was unsuccessful for the input "{ id: 61141a8345c9ba4338f2af20 }" of type Object in the "_id" path of the "LeaveTypes" model

In an attempt to develop a Human Resource Management (HRM) project using Node and Mongodb (with Mongoose), I have implemented a leave management system consisting of two documents. The first document includes leave types such as annual leave, maternity lea ...

Bringing in a legacy ES5 module for integration within a ReactJS component

Attempting to incorporate an ES5 module into a new ReactJS application has been quite the challenge. I'm struggling with understanding the correct way to import the module so that the main function within it can be accessed and executed. Currently, m ...

Having trouble establishing a connection with the deployed express API

I am experiencing an issue with my API deployed on Heroku. Whenever I try to access it either from my localhost or my deployed frontend, I am receiving a fail status and the following details in the headers: Request URL: my-url Request Method: POST Status ...

When refactoring docxtemplater on the server, the request JSON object is no longer accessible in the doc.setData() method

I've been working on refactoring some code in my Node server to remove docxtemplater. The req.body json object is functioning properly and I can even print it inside the doc.setData() method. However, when I try to launch the server, I encounter this ...