"What is the best way to retrieve the name of the object passed in a function in

After searching high and low, I still can't seem to find the answer to my simple question. Maybe it doesn't exist, but I won't give up just yet.

So here's the scenario:

I created a global prototype in Vue, essentially a global class, with a function inside. When I pass my object to it and try to retrieve the object's name, I come up empty.

Here's the code snippet:

// index.vue
export default {
  data() {
    return {
      portfolio: {
        portfolioID: null
      }
    }
  },
  async mounted() {
    // set model to new data                 // send old model
    this.portfolio = await this.$content.get(this.portfolio)
  }
}

// other file
export default class Content {
    static async get(content_type) {
        if (typeof content_type == 'object') {
            // [need to get the output of `portfolio`] out of the
            // content_type object
        }
        else { return { message: 'Input needs to be a model' } }
    }
}

I'm not interested in the keys of the portfolio object. I need the actual output of 'portfolio' in the get function of the content class.

This is currently my response in the get function from the content_type object:

{
   portfolioID
}

but what I'm hoping for is:

portfolio: {
  portfolioID
}

or at the very least, to retrieve the name 'portfolio'

Answer №1

Is it possible for you to create a demonstration on a platform like CodePen?

let dynamicImport = (keyName) => ({
  async mounted() {
    if (keyName in this) {
      this[keyName] = await this.$content.get({[keyName]: this[keyName]});
    } else {
      throw new Error();
    }
  }
});

export default {
  mixin: [
    dynamicImport("portfolio")
  ],
  data() {
    return {
      portfolio: {
        portfolioID: null
      } 
    }
  }
}

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

The express gateway is unable to transfer multipart/formdata

I've implemented express gateway as my main service gateway. One of the services I have needs to update an image, and when I try to handle files independently using multer it works fine. However, once this service is routed through express gateway, th ...

Redirecting JavaScript form to search engine

I am struggling with creating a form that enables a user to input text and then directs them to a specified search engine with the input as the query. I am encountering difficulties in getting the JavaScript to properly redirect. An interesting observatio ...

Issue with Rails: Content_For not functioning properly when combined with AJAX or when attempting to rehydrate JavaScript files

Currently, I am utilizing ajax to load all my views, and it's functioning perfectly except for one issue. My view pages that are being loaded are not referencing my JavaScript files. Below is an example of my coffee-script: jQuery(function() { Stri ...

Error message: "An unexpected character '<' was found at the beginning of the JSON data while loading a glTF file in a Three.js

Trying to incorporate a .glTF file into a Three.js project using the GLTFLoader in my Vue application has been presenting challenges. Each attempt at loading the .gltf file with the GLTFLoader results in the following error being displayed in the console: ...

Can the AngularJS icon adapt to different lists?

I'm currently developing in AngularJS / Jade and I need to implement functionality where an icon changes when a user clicks on something. The code I have right now looks like this: a(onclick='return false', href='#general', data-t ...

A helpful guide on resetting ReactJs to its default state when data is not found

Currently, I'm fetching data from my database, but just for the sake of this question, I have opted to manually create an example with fake data. I am in the process of creating a search bar for my users to navigate through all the data retrieved fro ...

React navigator appears stuck and unable to navigate between screens

I encounter an issue where my app closes when I press the switch screen button, but there is no error output displayed. I have checked for any version discrepancies and found no problem. The function I implemented for the button is functioning as expected ...

failure of text to display in d3.append

Having trouble displaying a tooltip on a rectangle in d3js. The tooltip renders, but the text doesn't show up. After researching similar issues, I discovered that I cannot directly append 'text' to a 'rect' element. I tried addin ...

Error message "$injector:unpr" occurs in the run method of AngularJS after minification process

I've encountered an issue with angular-xeditable on my Angular app. While it functions properly in the development environment, I'm facing an error in production when all JS files are minified: Uncaught Error: [$injector:strictdi] http://errors. ...

What is causing my HTML to not recognize my Angular JS code?

Trying to dive into Angular JS, I wrote a small piece of code but for some reason, the HTML is not recognizing Angular JS. This is my index.html file: <!DOCTYPE HTML> <html ng-app="store"> <head> <link rel="stylesheet" type=" ...

How can I create a Material ui Card with a border-less design?

After reviewing the information provided, I noticed that you can set the option to have it as variant="outlined" or raised However, I am curious if there is a method to create the card without any visible borders at all? ...

Having trouble updating a text field on an event using JavaScript - value not defined

When I change the select option, my goal is to dynamically set the value of the input using JavaScript. However, I am encountering an issue where the value becomes undefined. Below is a snippet from my HTML (JSP) file: <body> <center>< ...

substitute the character ""<0x00>"" within the given string

I'm currently facing an issue with a string I received after sending a command line that included the <0x00> character. How can I remove it from the string? For instance, here is my variable string: <0x00> awplus # desired output: awplus ...

Troubleshoot your Vue.js application using Visual Studio Code. Encounter an unidentified breakpoint error

I'm encountering a problem with debugging my Vue.js project using VS Code and Chrome. I followed the official guide on the website Guide, but it's not working for me. The error I keep running into is: unverified breakpoint What am I doing wrong? ...

What is the procedure for obtaining a Connect server's host name and port number?

Just like the example shown in this Express-related question, I'm wondering if there is a way to programmatically retrieve the host name and port number of a running Connect server? ...

Tips for utilizing Vue router query parameters while in hash mode:

Is there a more efficient way to access URL parameters in Vue methodology, without having to rely on window.location.href and parsing the URL? router/index.js const router = new Router({ mode: 'hash', routes: [] }); router.beforeEach((to, f ...

Guide to watching a particular property in an array of objects using Vue

I am currently working with Vue.js version 2.5.2 My goal is to monitor changes in the forms[*].selected array of objects and trigger a function when it changes. I attempted to achieve this by using a for loop to watch each object's selected property ...

What is the best way to retrieve a Rails variable that is restricted to a specific partial?

As a newcomer to Ruby on Rails, I find myself struggling to grasp the larger concept. Any assistance you can offer would be greatly appreciated. Within my application.html.haml file, I utilize =yield to pull content from ranked.html.haml. Currently, this ...

Implement a Bootstrap button that can efficiently collapse all elements in one click

Within my HTML file, I have included the following code: <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.4.1/css/bootstrap.min.css"> <div class="list-group list-group-flush"> <a href="javascript: void(0)" da ...

Changing a variable in an HTML file using a JavaScript file

I am working with JavaScript code that has been imported from another file containing a variable that I need to update in my HTML file. Is there a way to update the variable without directly inserting the JavaScript code into my HTML document? JavaScript ...