JavaScript error: Trying to access a variable that is not defined results in a ReferenceError

Can I access the variable named val1090 defined in validator.js from runesMetadata.js in nativescript vuejs?

This relates to metadata for raddataform in my case.

I encountered an error:

[Vue warn]: Error in data(): "ReferenceError: val1090 is not defined"
JS: found in
JS: ---> <Runes>
JS:        <NavigationEntry>

runesMetadata.js

import validator from "./validator";

export default {
  mixins: validator,
  data () {
      return {
        makeRuneMetadata: {
          'isReadOnly': false,
          'commitMode': 'OnLostFocus',
          'validationMode': 'Immediate',
          'propertyAnnotations':
          [ 
            {
              'name': 'r_makeRuneSpell',
              'displayName': 'Make rune spell',
              'index': 0,
              'editor': 'Text',
            },
            {
              'name': 'r_mpAbove',
              'displayName': 'If MP above [%]',
              'index': 1,
              'editor': 'Number',
               val1090, // <--------------------------
            },


      }
  }
}

validator.js

const val1090 = {
    'validators': [
        {
            'name': 'RangeValidator',
            'params': {
                'minimum': 10,
                'maximum': 90,
                'errorMessage': 'Value must be between 10-90.',
            }
        },
    ]
  }

export default val1090

**EDITED BELOW **

Adding more objects to the validator like this:

onst val1090 = {

            'name': 'RangeValidator',
            'params': {
                'minimum': 10,
                'maximum': 90,
                'errorMessage': 'Value must be between 10-90.'
            }  

};

const val115 = {

    'name': 'RangeValidator',
    'params': {
      'minimum': 1,
      'maximum': 15,
      'errorMessage': 'Value must be between 1-15.'
    }  

};


export default{
    val1090,
    val115
}

In my component:

created() {
    console.dir(val1090);
  },

The output will be:

==== object dump start ====
JS: val1090: {
JS:   "name": "RangeValidator",
JS:   "params": {
JS:     "minimum": 10,
JS:     "maximum": 90,
JS:     "errorMessage": "Value must be between 10-90."
JS:   }
JS: }
JS: val115: {
JS:   "name": "RangeValidator",
JS:   "params": {
JS:     "minimum": 1,
JS:     "maximum": 15,
JS:     "errorMessage": "Value must be between 1-15."
JS:   }
JS: }
JS: ==== object dump end ====  

How can I extract only the contents of the val1090 object? This is what I am looking for.

{
JS:   "name": "RangeValidator",
JS:   "params": {
JS:     "minimum": 10,
JS:     "maximum": 90,
JS:     "errorMessage": "Value must be between 10-90."
JS:   }
JS: }

Answer №1

When you export val1090 as default, it means that when you use the following import statement:

import validator from "./validator"

You are actually creating a variable named validator which holds the content exported by default in the validator file. This content looks something like this:

    {
     'validators': [
        {
            'name': 'RangeValidator',
            'params': {
                'minimum': 10,
                'maximum': 90,
                'errorMessage': 'Value must be between 10-90.',
           }
        },
    ]
  }

Therefore, in your runesMetadata.js file, make sure to reference validator instead of val1090. Alternatively, you can change your import statement to :

import val1090 from "./validator"

NOTE: If your validator file has more objects and you export them using export default, the imported variable will hold these values as well. For example, if you export:

export default {
    val1090,
    val115
}

And then import it like this:

import validator from "./validator"

You will essentially create a variable variable with the following content:

validator = {
    val1090: val1090,
    val115: val115
}

To access the value of val1090, simply refer to it as:

validator.val1090

Answer №2

Using just the property name in an object initializer, such as val1090, is a new ES6+ "shorthand" for val1090:val1090. For this to work, there must be a variable declared called val1090, which seems to be missing in your code. Check out the details on MDN docs for more information.

For example:

var a = 'foo', b = 42, c = {};
var o = {a, b, c};

This code snippet works because variables a, b, and c are declared.

var a = 'foo', b = 42;
var o = {a, b, c};

On the other hand, this code fails because variable c is not declared.

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

Utilizing modular structure for efficient jQuery AJAX request

Seeking guidance on optimizing multiple GET ajax calls, where each function contains repeated $.ajax({}) code lines. Is it feasible to create a single $.ajax({}) function and utilize it in all functions instead of duplicating the code? Perhaps something ...

Utilizing jQuery to seize events and handle AJAX callbacks

I am attempting to accomplish a simple task: capture any click event and send the URL to a PHP script. When using alert(a); the ajax.php is called every time. However, if I remove it, only once in every 20 clicks will work. I am wondering if this is due t ...

Issues with using a personalized font in a Stenciljs project

Looking for guidance on implementing a custom font in my Stenciljs app. I have the otf file, unsure if an npm package is necessary. Here's my code: filestructure: -src --components --assets ---Anurti-Regular.tiff ---Anurti-Regular.ttf friends-l ...

Delete auto-generated list using handlebars JS

I have successfully created a dynamic list using Handlebars.js and its template. However, I am now facing confusion on how to remove or delete items from the list using a function or specific code. As I am new to Handlebars, I would appreciate any help. ...

Multi-line input in ExtJs

Is there a way to create a multiline input with vertical scrollbars in EXTJS? I tried the following code: noteField = new Ext.form.TextField({ emptyText: 'note...', multiline: true, applyTo: &apos ...

Grid of pictures resembling a masonry pattern

As I ponder this intricate dilemma, I am convinced that there must be a simple solution or alternative approach to finding the answer. My goal is to create a grid of random images without any gaps between them. I have an array of images that I want to dis ...

What is the process for creating an xpath for hyperlinks with the 'contains text' attribute?

I am in need of creating Xpath for links based on contained text. I have devised the following code to achieve this, however, a scenario arises when there are three links named 'Entity', 'Entity Type', and 'Entity Group'. If I ...

Variable missing in the ExpressJs view

Hey there! I'm new to Nodejs and currently experimenting with it. I've been trying to convert some of my basic Python codes to JavaScript. In one of my projects, I am sending a get request to the YouTube API and receiving 50 results in JSON forma ...

Navigating tables with jQuery using a loop and extracting data from two tables with matching row names

I am facing a challenge with a function that combines data from two tables, each containing a column named "name". How can I specify which table's name should be displayed? function createTableRow(customers) { var data = JSON.parse(customers.resu ...

"Interactive jQuery feature allows users to edit table content live with the ability to update and delete data via

I am working on implementing live editing, updating, and deleting functionality in a table using jQuery. However, I have encountered two problems: When clicking the pen icon from order #3 to #1, it requires 3 clicks, but when clicking from order #1 to ...

I am wondering why the vue-router is only changing the URL and not displaying the corresponding view component

I've encountered an issue where the vue-router is only toggling the URL when clicking on the navigation link, but not updating the view component. Surprisingly, there are no apparent errors in my code. Despite this, the console indicates that the Home ...

I need help figuring out the proper way to establish an indexing path in cosmos db using the nodejs sdk

I'm currently facing a challenge with setting up the indexing policy for one of my cosmosdb containers. Within my cosmosdb, I have a container that stores information about user sessions. Using the node sdk, I am defining the containers, partition key ...

Guide on Importing All Functions from a Custom Javascript File

As a beginner in learning Angular, I am faced with the task of converting a template into Angular. However, I am struggling to find a solution for importing all functions from a custom js file into my .component.ts file at once. I have already attempted t ...

Conceal list items by clicking elsewhere on the page

Currently, I am facing an issue with my search user functionality. Whenever a user clicks anywhere else on the page, the list of results does not disappear unless the user manually deletes all the words they have typed. This behavior is not ideal and despi ...

The functionality of AJAX is hindered in the browser when attempting to fetch data from a URL

Lately, I've been encountering a strange issue when trying to fetch data from a URL. $.ajax({ url: 'URLHERE', dataType: 'html', success: function(data) { //function 2 var xml = $.parseXML(data) $(xml).find('StopLoca ...

What is the reasoning behind having two separate permission dialog boxes for accessing the webcam and microphone in flash?

I am currently using a JavaScript plugin known as cameratag () in order to record videos through the web browser. This plugin utilizes a flash-based solution. When the flash application requests permission to access the webcam, it presents a security dialo ...

The selected option is not visually highlighted in the ui-select due to a programming issue

angular version: AngularJS v1.3.6 http://github.com/angular-ui/ui-select : Version: 0.8.3 var p1 = { name: 'Ramesh', email: '[email protected]', age: 99 }; $scope.people = [ { name: 'Amalie', ...

Showing hidden errors in specific browsers via JavaScript

I was struggling to make the code work on certain browsers. The code you see in the resource URL below has been a collection of work-around codes to get it functioning, especially for Android browsers and Windows 8. It might be a bit sketchy as a result. ...

Passing slots to child components within a VueJS application - A helpful guide

Within my application, I frequently utilize a list and list_item component. To provide a general idea: contact_list.vue <template lang="pug"> .table .table-header.table-row .table-col Contact .table-col Info .tabl ...

Updates to mousetrap key bindings are being reflected in the $scope object, but are not being detected by the

I have developed a function that updates a scope variable as shown below: // controller.js $scope.variable = 0; $scope.$watch('variable', function(){ console.log("change from watch"); }); $scope.increment = function(){ $scope.variable++; ...