The function of the Nuxt.js server-side plugin is not functioning as intended

I recently developed a server-side plugin and encountered the following error:

context.app.handleServerError is not a function

// hanlde-server-error.js

export default ({ app }, inject) => {
  app.handleServerError = (method, error, data) => {
    const message = `An error occurred in ${method}. ${error}`
    console.error(message)
    Sentry.captureException(new Error(message))
  }
}

// nuxt.config.js

  plugins: [
    { src: '~plugins/handle-server-error', mode: 'server' },
  ],

// calling function

  async asyncData(context) {
    // await store.dispatch('fetchAccounts')
    try {
      await undefinedFunction()
    } catch (error) {
      context.app.handleServerError('asyncData', error, { user: 'bambam' })
    }
  },

Is it safe to assume that asyncData is executed server-side? As per the documentation, this function is supposed to be accessible via the context.

Answer №1

Run this code exclusively on the server side.

async asyncData(context) {
  if (process.server) {
    try {
      await undefinedFunction()
    } catch (error) {
      context.app.handleServerError('asyncData', error, { user: 'bambam' })
    }
  }
},

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

Navigating through choices in select element using webdriverio

Sample HTML: <select id="random_text"> <option value="option1">asadka_TEST</option> <option value="option2">Peter Parker</option> <option value="option3">Clark Kent</option> <option value="optio ...

The GET API is functioning properly on Google Chrome but is experiencing issues on Internet Explorer 11

Upon launching my application, I encountered an issue with the v1/validsColumns endpoint call. It seems to be functioning properly in Chrome, but I am getting a 400 error in IE11. In IE v1/validCopyColumns?category=RFQ&columns=["ACTION_STATUS","ACTIO ...

Load a JSON file into the app.js of your project

Could someone please help me figure out how to successfully load a JSON file into my project's app.js? I've tried using the following code snippet: var j = require('./JSON1'); However, it didn't work as expected. My goal is to la ...

The link function of an AngularJs directive is unable to access the attribute value

I am facing an issue with a directive in AngularJS. return { restrict: _restrict, link: function (scope, element, attrs) { $timeout(LinkPre, 0); //Calling a scoped method }, templateUrl: Con ...

Is there a way to update a div element with an image without having to refresh

Currently working on a project for my studies where I am reading the x and y coordinates from a touch pad and drawing it in real time using PHP. I have created a draw page that takes these coordinates and displays them on an image, which is then shown on ...

Leverage the data from a local JSON file within a web application built with Vue CLI 4.2.3

I need to use a JSON file to store some data and utilize them in the components. [ { "heroTitle": [ "Banner image", "assets/images/banner/sunset.jpg", "sunset.jpg" ], } ] The above JSON is an example, and below is my compone ...

Press the second form submit button after the completion of another Observable

Below is the unique process we aim to accomplish using solely RXJS Observables: Press Login button (form with username & password). We bind Observable.fromEvent with our form. Upon submitting the form, call loginUser() to send an http request to serv ...

Attempting to embed a script tag within an Inertia and Vue component

Can anyone help me with SSR rendering Schema data for my blog post using a Vue/Inertia component template? When I attempt to render the data, I encounter the following error: [plugin:vite:vue] Tags with side effect (<script> and <style>) are i ...

Why is it that the condition of being undefined or not functioning properly in state?

I am currently facing an issue with a piece of code I wrote in React JS. The state variable is not functioning as expected and even after modifying it upon button click, nothing seems to be working. After checking the console, I noticed that the state rema ...

What is the best way to obtain the post id when making a JavaScript Ajax request?

I am currently developing a website similar to Stack Overflow for practice purposes. I am currently working on implementing the voting system. My goal is to have an Ajax call triggered when the upvote or downvote button is clicked, sending parameters such ...

What is the best way to allow my limit to be as greedy as possible?

I'm facing an issue with the code below where it currently only matches MN. How can I modify it to also match KDMN? var str = ' New York Stock Exchange (NYSE) under the symbol "KDMN."'; var patt = new RegExp("symbol.+([ ...

Ensure the Image URL is valid before modifying the State in React/Next

This code snippet is written in React/Next.js with styled-components. Hey there, I have a component that displays a blog banner using a background-image. The URL for the image comes from a state variable that currently holds a default image path. const [b ...

Listener for resizing events in jQuery implemented in a separate script file

One issue I am facing is separating my jQuery code from my HTML page, especially when it comes to the resize event. I have an index.html file and a main.js file where I prefer to keep all of my jQuery code. The problem arises when the resize event does no ...

I am encountering an issue with my Laravel Vue application where it is not able to delete a record. Should I consider binding my button to ensure it redirects to the controller

Hello, I am facing an issue while working on my app that involves a simple CRUD operation. The problem is that the code does not delete a record when I click the delete button. I am new to Laravel and Vue, so I am still getting familiar with how these two ...

Navigating with Google Maps and Its Pointers

I've successfully integrated a JSON array of Marker positions into a Google map. Each marker has an associated infoWindow, which is also functioning as expected. However, I'm encountering an issue where clicking on a marker only displays the in ...

Is there a way to eliminate a wrapper object from each element in a JSON array using JavaScript?

My knowledge of JavaScript is limited and I am facing a particular issue. The JSON document at hand looks like this: { "forecast": [ { "day-1": { "forecast_date": "2017-11-23", "morning": { "weather": { " ...

Is it necessary for the scope to be separated?

In the document object model (DOM), I have two straightforward directives that are responsible for creating similar elements. Both directives have an isolated scope. Each directive has an ng-click attribute that calls a method to display a message. One d ...

Utilize Angular 8 to dynamically populate Input values with data pulled from an API

I need help with setting the input value dynamically using data from my API. Once I click send, I want it to be saved in the database. However, I am struggling to dynamically populate the input field. Can someone guide me on the right approach to achieve t ...

What is the best method for storing a JavaScript widget with analytics - should it be done dynamically or statically?

My widget comes with a customizable boot loader that is used on websites. The boot loader file retrieves the settings for the widget and generates it accordingly. Normally, the content of the bootloader file remains static unless there are modifications ma ...

How to pass the ng-repeat $index value as a parameter in AngularJS

Within the code snippet provided, there is a shell page index.html and a partial view currently utilized by two different controllers. The static data in AppController is connected to the list.html partial view and displayed as a table. In the JavaScript p ...