Typescript - Error in Parsing: Expecting an expression

I am currently working with Vue and TypeScript and have encountered a problem. How can I resolve it? Here is the code snippet in question:

private setTitle(systemConfig: any) {
    const systemConfigParse;
    let obj;
    systemConfigParse = JSON.parse(systemConfig);
    obj = (<any>systemConfigParse).find((item: any) => {
      return item.Code == "hospitalName";
    });
    this.hospitalName = obj.Value;
    obj = (<any>systemConfigParse).find((item: any) => {
      return item.Code == "systemName";
    });
    this.systemName = obj.Value;
    this.title = this.hospitalName + this.systemName;
  }

The error seems to be at this line

return item.Code == "hospitalName";
Even after removing the following lines of code:

 obj = (<any>systemConfigParse).find((item: any) => {
          return item.Code == "hospitalName";
        });
 obj = (<any>systemConfigParse).find((item: any) => {
          return item.Code == "systemName";
        });

The error persists. Could this be caused by eslint? Any suggestions on how to troubleshoot and fix this issue would be greatly appreciated. Thank you in advance.

Answer №1

If you want to streamline your code, consider the following approach:

function setHospitalTitle(config: string) {
    const configObject = JSON.parse(config);
    this.hospital = configObject.hospital;
    this.system = configObject.system;
    this.hospitalTitle = this.hospital + this.system;
}

Your data structure should resemble this example:

const jsonData = `{"hospital":"General Hospital", "system": "Health System"}`;
setHospitalTitle(jsonData);

Answer №2

The issue has been resolved

private updateTitle(settings: any) {
    const settingsParsed = JSON.parse(settings);
    let obj;
    for(const item in settingsParsed){
      const i = settingsParsed[item];
      if(i.Code==="hospitalName"){
        this.hospitalName = i.Value;
      } else if(i.Code==="systemName"){
        this.systemName = i.Value;
      }
    }
    this.title = this.hospitalName + this.systemName;
  }

I learned to use forof instead of forin from @devdgehog's suggestion

private updateTitle(settings: any) {
    const settingsParsed = JSON.parse(settings);
    let obj;
    for (const item of settingsParsed) {
      if (item.Code === "hospitalName") {
        this.hospitalName = item.Value;
        console.log(this.hospitalName);
      } else if (item.Code === "systemName") {
        this.systemName = item.Value;
      }
    }
    this.title = this.hospitalName + this.systemName;
  }

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 PhotoService (?) is facing difficulties in resolving dependencies according to Nest

Just started diving into Nest.js and encountering an issue right after setting up a service: Nest is throwing an error while trying to resolve dependencies of the PhotoService (?). Make sure that [0] argument is accessible in the current context. I' ...

What could be causing my Mocha reporter to duplicate test reports?

I've created a custom reporter called doc-output.js based on the doc reporter. /** * Module dependencies. */ var Base = require('./base') , utils = require('../utils'); /** * Expose `Doc`. */ exports = module.exports = ...

Has the web application continued to run in the background even after toggling between tabs on the browser?

Does the app continue running in the background even when I'm not on that specific tab? Or does it pause when I switch between tabs, requiring a new request to the server for any updated data from the database upon returning to that tab? Edit: Curren ...

Creating custom markers with an API in Vue-2-Leaflet

I'm having trouble displaying markers using an API. I can retrieve the data from the API and store it in a variable, but for some reason the markers aren't showing up when I try to display them using a v-for loop. Any assistance would be greatly ...

Ways to extract data from form inputs with the help of jQuery

My HTML form allows users to enter data and upon submission, I need to use jQuery to capture the entered values. <form class="my-form needs-validation" method="POST"> <input type="text" id="firstName" name="First Name"> <input type="tex ...

How to retrieve the latest document from every sender within a JavaScript array using Mongoose/MongoDB queries

I recently created a schema: var messageSchema = mongoose.Schema({ sender:String, recipient:String, content:String, messageType: Number, timestamp: {type: Date, default: Date.now} }); Next, I defined a model for this schema: var Mess ...

Is there a way in jQuery to identify if a paragraph contains exactly one hyperlink and no other content?

I need to assign the 'solo' class to a link within a paragraph only if that link is the sole element in the paragraph. This example would have the 'solo' class: <p><a>I am alone</a></p> However, this example w ...

Can integer values be stored in localStorage similar to JavaScript objects and retrieved without requiring typecasting?

After setting an integer value to a localStorage item: localStorage.setItem('a', 1) and checking its data type: typeof(localStorage.a) "string" it shows as a string. I then typecast it to an int for my purposes: parseInt(localStorage.a) My ...

The Textfield component in Material UI now automatically sets the default date to the current date when using the "date" type

I am using Material UI's textfield with the type set to "date" and I'm experiencing an issue where the date defaults to the current date instead of mm/dd/yyyy. Is there a way to prevent this behavior and display mm/dd/yyyy when the user loads the ...

What is the method for getting js_xlsx to include all empty headers while saving the file?

In the midst of developing a Meteor App, I've incorporated the Node.js package known as "js_xlsx" from "SheetJS", produced by "SheetJSDev". This tool enables me to convert an Excel sheet uploaded into JSON on the backend. The intention is to store thi ...

Enhancing a React Native application with Context Provider

I've been following a tutorial on handling authentication in a React Native app using React's Context. The tutorial includes a simple guide and provides full working source code. The tutorial uses stateful components for views and handles routin ...

Populate a map<object, string> with values from an Angular 6 form

I'm currently setting keys and values into a map from a form, checking for validation if the field is not null for each one. I am seeking a more efficient solution to streamline my code as I have over 10 fields to handle... Below is an excerpt of my ...

The error message "prettyPrint is not defined" indicates that the function prettyPrint

I am facing an issue with ReferenceError: prettyPrint is not defined. Can you provide some help? <a class="question helpcenterheading" href="http://www.google.com">How do I reach out to you?</a> <span class="answer">Just a moment...</ ...

Passing Data from $http.get to Angular Controller Using a Shared Variable

One issue I'm facing is the inability to pass the content of a variable inside $http.get() to the outside scope, as it always returns undefined. I attempted using $rootScope, but that approach was not successful. controller('myControl', fu ...

Steps to customize the color scheme in your Angular application without relying on external libraries

Is there a way to dynamically change the color scheme of an Angular app by clicking a button, without relying on any additional UI libraries? Here's what I'm trying to achieve - I have two files, dark.scss and light.scss, each containing variabl ...

Searching for the name of dynamically generated input fields using jQuery

I have a straightforward form featuring radio buttons <form> <input type="radio" name="radio_1" value="1" />Radio 1 <input type="radio" name="radio_1" value="2" />Radio 2 <input type="radio" name="radio_1" value="3" />Radio 3 </ ...

Is there a way to use jQuery to enable multiple checkboxes without assigning individual IDs to each one?

I need help finding a way to efficiently select multiple checkboxes using jQuery without relying on individual ids. All of my checkboxes are organized in a consistent grouping, making it easier for me to target them collectively. To illustrate my issue, I ...

Refresh the Node.js page to generate fresh data upon reloading the page

I am currently running a web page on Node.js using the default folder setup provided by WebStorm, and I am utilizing ejs for rendering pages. To start the server, I run node bin/www with the following code snippet: ***preamble*** var app = require('. ...

Error: The parent class is not defined as an object

My program is currently attempting to utilize this.state, but I encountered an error. Can anyone provide assistance on resolving this issue? https://i.stack.imgur.com/wgxBf.png ...

Using a variable in Ajax URL with Action razor syntax

I have a function that calls a method in the controller through an Action URL. However, I need to use a parameter as the name of the method, but unfortunately, this is not possible in the current implementation. function changeDropDownList(id, dropNameToC ...