The .map() operator requires a declaration or statement to be specified - TS1128 error

I've tried various solutions from different sources but none seem to be resolving the issue I'm facing. The problem is: when trying to run my app, I encounter the following error:


10% building modules 0/1 modules 1 active …\src\app\contacts\contacts.component.tsERROR in src/app/contact.service.ts(18,7): error TS1128: Declaration or statement expected. src/app/contact.service.ts(27,7): error TS1128: Declaration or statement expected. src/app/contact.service.ts(33,7): error TS1128: Declaration or statement expected.


    import { Injectable } from '@angular/core';
    import { Http, Headers, Response} from '@angular/http';
    import { Contact } from './contact';
    import 'rxjs/add/operator/map';
    import { map } from 'rxjs/operators';

            @Injectable({
      providedIn: 'root'
    })
    export class ContactService {

        constructor(private http: Http) { }

        // Retrieving contacts
        getContacts() {
          return this.http.get('http://localhost:3000/api/contacts');
          .map((res: any) => res.json());

        }

        // Adding contacts
        addContact(newContact) {
          var headers = new Headers;
          headers.append('Content-Type', 'application/json');
          return this.http.post('http://localhost:3000/api/contact', newContact, {headers: headers});
          .map((res: Response) => res.json());
        }

        // Delete contact method
        deleteContact(id) {
          return this.http.delete('http://localhost:3000/api/contact' + id);
          .map((res: Response) => res.json());
        }
    }

The line containing

.map((res: any) => res.json());
indicates:

declaration or statement expected.

Answer №1

It is important to note that you have included a ; before the .map function call in your code snippet. The correct implementation should look like this:

return this.http.get('http://localhost:3000/api/contacts')
     .map((res: any) => res.json()); 

The presence of the semicolon causes a premature end of the statement, resulting in a syntax error for the subsequent .map function.

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

Customize the theme type with @mui/system

Is there a way to customize my theme settings in @mui/system? While using the sx prop in Stack, the theme is defined in createTheme.d.ts, but it seems like there isn't an option to extend or override it. To work around this limitation, I have been u ...

Is it possible to pass an external function to the RxJs subscribe function?

Upon examining the RxJS subscribe method, I noticed that: subscribe(next?: (value: T) => void, error?: (error: any) => void, complete?: () => void): Subscription; So, I decided to create an example initialization function like this: private ...

How can I access DOM elements in Angular using a function similar to the `link` function?

One way to utilize the link attribute on Angular 2 directives is by setting callbacks that can transform the DOM. A practical example of this is crafting directives for D3.js graphs, showcased in this pen: https://i.sstatic.net/8Zdta.png The link attrib ...

Tips for adjusting the time format within Ionic 3 using TypeScript

I currently have a time displayed as 15:12:00 (HH:MM:SS) format. I am looking to convert this into the (3.12 PM) format. <p class="headings" display-format="HH:mm" > <b>Time :</b> {{this.starttime}} </p> In my TypeScript code t ...

There is no universal best common type that can cover all return expressions

While implementing Collection2 in my angular2-meteor project, I noticed that the code snippets from the demo on GitHub always result in a warning message being displayed in the terminal: "No best common type exists among return expressions." Is there a ...

When the value is empty, MUI Autocomplete will highlight all items

I have encountered a specific issue. I am working on developing a custom Autocomplete Component for filtering purposes. However, I recently came across the following Warning. MUI: The value provided to Autocomplete is invalid. None of the options matc ...

Switching from file:// to http:// in Angular / Ionic is a necessary step when incorporating external scripts like the Disqus directive into your project

Currently, I am attempting to incorporate the disqus directive into my project. The documentation for this directive can be found here. However, I have encountered some issues due to the particular setup of my application. There is a line in the script wh ...

What is the most effective way to implement multiple ng-models within a single function?

Hello everyone! Currently, I am working on developing a database using indexed db in AngularJS. My main task is to save data into the database and I have a query - Can we utilize multiple ng-models in a single function? Let me provide you with a snippet of ...

Angular HttpClient Catch Return Value

In my quest to develop a universal service for retrieving settings from the server, I've encountered an issue. When errors arise, I want to intercept them and provide a default value (I have a predetermined configuration that should be utilized when e ...

Creating React components dynamically using the number of objects passed as props

When attempting to create components based on the number specified in an object's files property, I keep encountering an error indicating that the parent component has too many children. If the files property is set to 5, does anyone have a solution ...

Forwarding AngularJS events using socket.io with the use of deferred promises

I recently started using the Angular library angular-socket-io for my project. However, I encountered a challenge where I needed to implement authentication within the app before initializing the socket.io interface. To address this issue, I found and impl ...

Hold off until the RxJS dispatch is resolved

I am working on integrating a "next step" feature into my Angular 6 webapp. When the user clicks the "next step" button, the frontend triggers an action to update the database with the data in the store, another action to retrieve processed data from a Spr ...

How can we leverage mapped types in TypeScript to eliminate properties and promisify methods?

Below is the provided code snippet: class A { x = 0; y = 0; visible = false; render() { return 1; } } type RemoveProperties<T> = { readonly [P in keyof T]: T[P] extends Function ? T[P] : never//; }; type JustMethodKe ...

Is it possible to turn off Angular CLI ng build linting for a specific directory?

I am facing an issue with a specific directory in my project template that I want to exclude from linting. Despite excluding it in both tsconfig and eslint, running eslint works fine but when using ng build, the directory is still included in linting and e ...

Tips for launching Nx serve in debug mode for Angular using VSCode

When running my Angular Nx project in the VSCode debugger, I encounter an issue with using yarn. yarn start successfully executes the nx serve command when run from a terminal. However, the same yarn start command fails when executed through VSCode debug ...

AngularJS: Troubleshooting ng-switch malfunction

I've been exploring various methods to show an HTML content block based on a selection. Instead of using ng-include, I have considered options like ng-if and ng-show/hide. I came across ng-switch and saw that it was working in a demo, but I'm f ...

What is the mechanism Angular utilizes to determine the exact location of directives within a webpage?

Can you explain how Angular is able to locate the directives on a webpage and establish connections with or observe those elements? I've searched through the DOM reference, but it seems like methods like getElementbySomething and querySelectorAll may ...

The dropdown navigation bar fails to close upon being clicked

I'm currently facing an issue with the navbar in my project. The bootstrap navbar doesn't close automatically after clicking on a link. I have to move my mouse away from the navbar for it to close, which is not ideal. Additionally, I'm worki ...

angular directive to receive an object

When I have a table and click on a row, the information is supposed to be displayed in a different component using the @input decorator. However, instead of displaying the correct result in my other component, I am getting [object Object]. table.html < ...

Material-UI React Native components: Injecting Styles without Props

import {createStyles, WithStyles} from "@material-ui/core"; const styles = (theme: Theme) => createStyles({ root: {} }); interface MyProps extends WithStyles<typeof styles> { } export class MyComponent extends Component<MyProps ...