Return of Request - Angular4

Asked

Viewed 1,368 times

1

I started the studies with angular4, and I’m trying to get the return json a simple request through a service.

But when calling the method that makes the request, it presents me the following error in my component login.component.ts:

Property 'subscribe' does not exist on type '() => void'.

However, by executing that same method within my component it works normal and returns me mine json

Below follows the code of mine service and my component:


credentials.service.ts

import { Injectable } from '@angular/core';
import { Http } from '@angular/http';

import 'rxjs/add/operator/map';

@Injectable()
export class CredentialsService {

  constructor(private http: Http) { }

  public getCredentials(){
    var url = 'http://luingry.com.br/easyjob/WebServices/angular/login.php';

    this.http.get(url)
    .map(dados =>{ return dados.json();});
  }

}

login.componentts.

import { Component, OnInit } from '@angular/core';

//importando o serviço
import { CredentialsService } from './services/credentials.service';

@Component({
  selector: 'app-login',
  templateUrl: './login.component.html',
  styleUrls: ['./login.component.css']
})
export class LoginComponent implements OnInit {

  constructor(public c: CredentialsService) {}

  ngOnInit() {
    this.c.getCredentials.subscribe(g =>{
      console.log(g);
    });
  }

}

What to do to get the json of my request through service.

1 answer

0


Only missing the Return on the service http (so the typescript interprets that you return a value-free observable):

public getCredentials(){
    var url = 'http://luingry.com.br/easyjob/WebServices/angular/login.php';
    /* return aqui */
    return this.http.get(url)
    .map(dados =>{ return dados.json();});
  }
  • Our dear, my silly... I saw that it was problem with Return but I didn’t even care that my function was not returning anything.

  • Maybe what confused you is map Return... I usually leave the Arrow Function of a single... command without Return. map(data => data.json());

  • I get it... but then I put one console.log() outside the subscribe and it presents the result undefined, only when I put the console.log within the subscribe the same presents me the json... Do you know what this is about? Do you want me to open another question so you can help me?

  • I’m not sure I understand your question... It happens because the call is asynchronous... Subscribe only signs the http event by passing a function in the parameter and this function is only executed when the http result returns... By placing off the subscribe you will ask for the variable g that doesn’t even exist not context from outside, so Undefined back

  • In case then I have to do user and password validation inside the subscribe? Or is there some more right way?

  • 1

    In the case of authentication the right thing is to do all the security validation on the server side, because the front is totally manipulable by the client. The site must be served with https and have an authentication service that receives the user and password posted within the body of the post. After sending the user and password in this secure channel, the server must provide a randomly generated access token and store it in some resource to relate the user to the generated token. In the client inside the subscribe store the token and post in every request. The token check must be on the server requiring login if it expires.

  • Take a look at this post that explains a cool jwt authentication stream with PHP server. Just be careful with the password post in the URL, don’t do it... Place in the body of the message to be encrypted in https: https://stackoverflow.com/a/29171466/5108174

Show 2 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.