Issue with accessing the user parameter in before hook in FeathersJs

Whenever I attempt to assign user._id to an entity in a before hook, I keep receiving an "undefined" message.

Below is the code for the hook:

module.exports = (options = {}) => { 
return async context => { 

const user = context.params.user;

context.data = {
...
      userId: user._id,
...
    };
return context; 
}; 
};

This snippet shows my hooks register:

  before: {
...
    create: [processProperty(), authenticate('jwt')],
...
  }
}

Answer №1

After rearranging the order of my hooks, I successfully resolved the issue. To ensure that my processProperty hook can access the authenticated user, I moved the authenticate hook before it in the array. This adjustment was necessary because each hook is processed from left to right.

  before: {
...
    create: [authenticate('jwt'), processProperty()],
...
  }
}

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

Manipulating JSON objects within a map using jQuery and Gson

I am working with a Java object that contains several nested object fields with basic fields in them. Here is an example: Template { String name; EmailMessage defaultEmailMessage; } EmailMessage { String emailSubject; String emailBody; } ...

Can fog be applied from a fixed location within a three.js scene without being tied to the camera's position?

Is it feasible to render fog in such a way that an avatar on a terrain remains clear while the surrounding area gradually fades into the mist, especially when the camera is positioned overhead at a distance? ...

Out of Sync Promise Chain

I recently encountered an issue with promise chaining in JavaScript, specifically while working with Vue.js. Here is my code: I have an addItem function that inserts an item into the database. My goal is for this function to first insert the data into the ...

Popper.js failing to initialize during page load

After attempting to initialize Popper.js as shown below, I am encountering an issue where nothing is happening. $().ready(function(){ $('[data-toggle="tooltip"]').tooltip(); }); Interestingly, when I apply the same code to a button, it w ...

"Encountered a Http502 error while running the Node component (code provided for reference purposes

Encountering the Http502 error while developing a node component for chatbot. The first code snippet works flawlessly, but the second one triggers an Http502 error. Both snippets share the same host and proxy settings, with only the endpoint being differen ...

What could be causing the errors in my subscription function?

Working on an e-commerce website, I encountered errors in the "cartservice" specifically in the "checkoutFromCart()" function. The console displayed the following error: src/app/services/cart.service.ts:218:81 218 this.http.post(${this.serverUrl}ord ...

Is it possible for a Node.js server to specifically generate dynamic HTML, with Nginx handling the distribution of static data, and then automatically deliver the content to the client?

After primarily working with Apache and PHP, I've recently started exploring Nginx and Node.js and have been really enjoying the experience. Initially, I set up an Express server to handle website files and HTML rendering using Handlebars. However, I ...

Verify if the element's visibility can be confirmed by utilizing the IntersectionObserver function

I am currently working on my next.js component layout and trying to determine when a modal window containing the div #snipcart becomes visible on the screen. I have attempted to utilize the IntersectionObserver for this purpose, but I am not receiving any ...

Guiding the user towards a sub-state while transitioning to the main state by utilizing UI-Router

Take a look at this: .state('manager.staffList', {url:'^/staff?alpha', templateUrl: 'views/staff.list.html', data:{activeMenu: 'staff'}, controller: 'staffListCtrl'}) .state('manager.staffDetail', ...

Issue in Three.js: Unable to modify Boolean value within the onLoad function

I'm utilizing the Three.js Loading Manager to control the initiation and halting of my requestAnimationFrame animation cycle. Here's the code snippet: var loadingManager = new THREE.LoadingManager(); var isAnimationRunning = new Boolean(); ...

An HTTP OPTIONS request is sent prior to the actual request being made

As I work on developing the backend using node.js and mongoDB, focused primarily on API calls, I have noticed an interesting pattern. Every time I make a PATCH or DELETE API call, there is always an OPTIONS API call with the same URL right before it. The o ...

Placing a hyperlink following the statement document.getElementById('').innerHTML =

I have created a countdown timer and once it finishes, I want it to display "Task Completed, click here to proceed." The "Click here" should redirect to a hyperlink. However, I am unsure how to implement this functionality. Here is the Javascript cod ...

Unable to remove data once it exceeds 10 entries while using AJAX, jQuery, and JavaScript/PHP

I am having an issue where I can insert data into the first 10 rows and delete any of them successfully. However, when I try to insert data starting from the 11th row, I am unable to delete any rows past the 10th. UPDATE: The deletion function does not wo ...

Enhance the keyup search function to accept the input value as well

I currently have a keyup search function that works when a user types directly into the field. Here is the code snippet: if(this.$search_ele.length){ this.has_search = true; this.searchFn = this.buildSearchFn(opts.fields); this.bindEv ...

Update the displayed locations on Google Maps by fetching and displaying marker data

I am able to retrieve and display information from my MySQL table, but I need assistance with refreshing this data every 5 seconds using my current code. The data being shown is not extensive, just about 5 or 8 markers at a time. Below is the code I curren ...

Issues detected in the performance of custom Express Server on NextJs

Switching to NextJs has been a challenge for me. I have developed an ExpressJs API and now I want to integrate it as a Custom NextJs Server, following the documentation. Unfortunately, I can't seem to get it working properly. Below is my server.ts fi ...

JavaScript - Need to automatically scroll to a different div when scrolling occurs

Currently, my focus is on creating a single-page website where the main content is displayed in large boxes arranged vertically down the page. If you have any suggestions or thoughts on using JavaScript to navigate to each content box more efficiently, I ...

What is the best way to incorporate a function that works seamlessly with elements?

For the elements with corresponding 'li' that has a selected class, I need to set the selected property only for those specific 'option' elements. The value is determined by the data-input-value attribute in the 'li' and the v ...

What is the reason behind the android code being incompatible with IOS operating systems?

I'm encountering an issue where a particular code is working on Android, but not on IOS. The code in question involves using JavaScript to differentiate the items in a list within an HTML select tag. It's perplexing to me how the code can operat ...

Node and Backbone server facing CORS issue: POST requests unsuccessful despite receiving Options response with 200 OK status code, due to Access-Control-Allow

I'm currently facing an unusual issue. My app uses Backbone on the front end and Node/Express on the back end. After adding a middleware in my Node server to allow CORS requests, the OPTIONS request returns a 200 OK status, but the POST request does ...