Connecting a variable to a specific property of an object

Inside a file named controllers.js, I have declared the following variables :

var indexCtrl           = require('./index'),
    offerCtrl           = require('./offer'),
    setLocaleCtrl       = require('./setLocale'),
    specificationsCtrl  = require('./specifications');

and also this object :

var routes = {
    indexCtrl: {
        'fr-FR' : '/accueil',
        'en-EN' : '/home',
        'de-DE' : '/startseite'
    },
    offerCtrl: {
        'fr-FR' : '/offre',
        'en-EN' : '/offer',
        'de-DE' : '/angebot'
    },
    setLocaleCtrl: {
        'fr-FR' : '/setLocale/:locale',
        'en-EN' : '/setLocale/:locale',
        'de-DE' : '/setLocale/:locale'
    },
    specificationsCtrl: {
        'fr-FR' : '/specifications',
        'en-EN' : '/specifications-en',
        'de-DE' : '/spezifikationen'
    }
};

To illustrate, here is an example of the required index.js :

'use strict';

var IndexModel = require('../models/index');

exports.route = function(req, res, locale) {
  console.log(locale);
  var model = new IndexModel();
  res.render('index', model);
};

How can I utilize the indexCtrl defined within the var route as the indexCtrl declared above (with the require) ?

EDIT : For instance :

for (var ctrl in routes) {
    var ctrlContent = routes[ctrl];
    for (var language in ctrlContent) {
        var route = ctrlContent[language];
        //router.get(route, ctrl);
        console.log('%s, %s, %s', ctrl, language, route);
    }
}

The log output displays :

indexCtrl, fr-FR, /accueil
indexCtrl, en-EN, /home
indexCtrl, de-DE, /startseite
offerCtrl, fr-FR, /offre
offerCtrl, en-EN, /offer
offerCtrl, de-DE, /angebot
setLocaleCtrl, fr-FR, /setLocale/:locale
setLocaleCtrl, en-EN, /setLocale/:locale
setLocaleCtrl, de-DE, /setLocale/:locale
specificationsCtrl, fr-FR, /specifications
specificationsCtrl, en-EN, /specifications-en
specificationsCtrl, de-DE, /spezifikationen

This reveals that the route /accueil should be displayed in the fr-FR locale using the controller indexCtrl. However, attempting to use ctrl.route - like indexCtrl.route- which is specified in the require statement results in an error message :

Route.get() requires callback functions but got a [object Undefined]
.

Answer №1

Upon reviewing your inquiry, here is a potential solution:

let indexController           = require(routes.indexCtrl[language]),
    offerController           = require(routes.offerCtrl[language]),
    setLocaleController       = require(routes.setLocaleCtrl[language]),
    specificationsController  = require(routes.specificationsCtrl[language]);

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 placeholder within my input moves up and down when switching the input type from password to text

Currently, I am encountering an issue with the styling of a standard input element in React. Specifically, the placeholder text moves up and down by about 2px when viewed on Chrome, while there are no problems on Safari. How can I go about resolving this i ...

How to Retrieve Child Class Property from Static Parent Class Method in JavaScript

How can we implement a parent class with static methods that access a property defined in the child class using JavaScript? Below is an example code snippet illustrating this concept: class Parent { static greet() { return `Hi ${this.usernam ...

I am looking to narrow down the Google Places autocomplete suggestions specifically for India within Next Js

In my current project developed with Next.js, I am utilizing the react-places-autocomplete package to enhance user experience. One specific requirement I have is to filter out location suggestions for India only, excluding all other countries. Despite att ...

The length of the JavaScript array is not accurate

In my code, there is an array named 'plotData' which contains multiple 'rows' of data, each represented by a 4-element array. The array 'plotData' gets updated by a $.post() function further down in the script. The placeholder ...

Is it possible to reuse variables declared in a Javascript function within the same function?

string empCode = ds.Tables[0].Rows[i]["EMP_CODE"].ToString(); string empName = ds.Tables[0].Rows[i]["EMP_NAME"].ToString(); string gradeCode = ds.Tables[0].Rows[i]["GRADE_CODE"].ToString(); tr = new TableRow(); td = new Ta ...

I need to access the link_id value from this specific actionid and then execute the corresponding function within the Ionic framework

I have a JavaScript code in my TypeScript file. It retrieves the attribute from a span element when it is clicked. I want to store this attribute's value in a TypeScript variable and then call a TypeScript function. Take a look at my ngOnInit method, ...

Transferring information from an external JavaScript file to a Vue component

I am currently facing an issue with passing data from an external file called data.js to the component for its usage. The error I'm encountering states that the data is not defined. Below is the content of my data.js file: const data = [ { cha ...

Dealing with the selection of multiple options and checkboxes with identical names

I've encountered an issue when trying to send multiple values from a form to the backend. Here are my questions: How can I retrieve all the values selected in a multiple select field on the backend? What is the process for getting different values ...

Unable to Add Dependency to Subclassed Object

In my setup, there are three classes that interact with each other. I am utilizing inversify for handling dependency injection. However, I encountered an issue when I injected the class MessageBroker into the derived class Repository and found that the Mes ...

Should I include one of the dependencies' dependencies in my project, or should I directly install it into the root level of the project?

TL;DR Summary (Using Lodash as an example, my application needs to import JavaScript from an NPM package that relies on Lodash. To prevent bundling duplicate versions of the same function, I'm considering not installing Lodash in my application' ...

Bizarre actions with jQuery append in Internet Explorer 8

Issue with adding elements to a div in IE8: The element is not added until the button is clicked twice, resulting in two new elements being added at once. Here's the code snippet in question: $(options.addButton).click(function(event) { var proto ...

Include a condition to verify the values

Looking to enhance the code by incorporating an if or switch case: The initial code snippet is as follows: {(errorMessage || invalidEmailError.length > 0) && ( <Message type="error" className="mt-l" ...

Discover the index of the row when the value in the dropdown list is updated

I'm faced with a challenge regarding an HTML Table that contains a dropdown list in every row. I would like the background of each row to change whenever the value in the dropdown list is modified. Below is the code snippet: <table id="table1"> ...

I am encountering an issue where JSON.parse is returning undefined when trying to

Upon receiving a string from my server, I am attempting to parse it into a JSON object. Here is the string and relevant code snippet: stringToParse = "\"{'female': 16, 'brand': 75, 'male': 8}\"" d ...

Issues with Ajax response being added to the table

I am encountering a technical problem with my university project. The task assigned by the professor is as follows: We need to create a static table that lists 3 domain names in order to enhance the performance of the domain availability API. <table&g ...

Why does the arrow function get executed right away once it is wrapped?

Currently, I am working on developing an app using react native. The goal is to have the screen automatically advance to the next one if there is no touch response within 5 seconds. I have implemented a function where pressing a button or entering text wi ...

What is the best way to add timestamps to derivative posts or comments related to a post?

Currently working on a tutorial and I think it would be great to timestamp comments as well. For the main posts, the timestamp works fine, but not for the child posts or comments as we can call them for simplicity. Since I am using Firebase, I'm uns ...

"Learn how to showcase a picture in full-screen mode when the webpage is opened

I recently came across a script on Stack Overflow that allows me to select an image at random from an array. The script can be found here: Script to display an image selected at random from an array on page load However, I want to take this concept furthe ...

Updating textures dynamically for individual faces in a three.js environment

I have been facing a difficult challenge for some time now, struggling to achieve my goal while unsure if I am on the right path. The Objective My current project involves developing a three.js web application that loads JavaScript models and assigns cu ...

Issue with activating a Modal through a button inside a table row on React

I'm currently working on two files: Modal.js and Users.js. Users.js features a table with an API get query linked to it, and in the last column of the table, there's a dropdown for each row that contains three buttons: View, Edit, and Delete. My ...