Runtime Error Cannot read property 'username' of undefined in ionic 3 - angularjs

I was using the following code in restful API with ionic 3 for an app am building. It was working properly and then started showing Run time Error Cannot read property 'username' of undefined.
These are the codes I wrote for the app.
this is my auth-service.ts
import { Injectable } from '#angular/core';
import { Http, Headers } from '#angular/http';
import 'rxjs/add/operator/map';
let apiUrl ='http://localhost/PHP-Slim-Restful/api/';
/*
Generated class for the AuthServiceProvider provider.
See https://angular.io/docs/ts/latest/guide/dependency-injection.html
for more info on providers and Angular DI.
*/
#Injectable()
export class AuthServiceProvider {
constructor(public http: Http) {
console.log('Hello AuthServiceProvider Provider');
}
postData(credentials, type) {
return new Promise((resolve, reject) => {
let headers = new Headers();
this.http.post(apiUrl + type, JSON.stringify(credentials), {headers: headers})
.subscribe(res => {
resolve(res.text());
}, (err) => {
reject(err);
});
});
};
}
and my signup .ts
import { Component } from '#angular/core';
import { IonicPage, NavController} from 'ionic-angular';
import { LoginPage } from '../login/login';
import { TabsPage } from '../tabs/tabs';
import { AuthServiceProvider } from '../../providers/auth-service/auth-service';
/**
* Generated class for the SignupPage page.
*
* See http://ionicframework.com/docs/components/#navigation for more info
* on Ionic pages and navigation.
*/
#IonicPage()
#Component({
selector: 'page-signup',
templateUrl: 'signup.html',
})
export class SignupPage {
responseData : any;
UserData={"username":"","password":"","email":"","name":""}
constructor(public navCtrl: NavController, public authService: AuthServiceProvider) {
}
ionViewDidLoad() {
console.log('ionViewDidLoad SignupPage');
}
signup(){
this.authService.postData(this.UserData,'signup').then((result) => {
this.responseData = result;
console.log(this.responseData);
localStorage.setItem('userData', JSON.stringify(this.responseData));
this.navCtrl.push(TabsPage);
}, (err) => {
// Error log
});
}
login(){
//Api connections
this.navCtrl.push(LoginPage);
}
}
this is the signup.html code
<!--
Generated template for the SignupPage page.
See http://ionicframework.com/docs/components/#navigation for more info on
Ionic pages and navigation.
-->
<ion-header>
<ion-navbar>
<ion-title>signup</ion-title>
</ion-navbar>
</ion-header>
<ion-content padding>
<ion-list>
<ion-item>
<ion-label floating>Name</ion-label>
<ion-input type="text" value="" [(ngModel)]="UserData.name"></ion-input>
</ion-item>
<ion-item>
<ion-label floating>Email</ion-label>
<ion-input type="text" value="" [(ngModel)]="UserData.email"></ion-input>
</ion-item>
<ion-item>
<ion-label floating>Username</ion-label>
<ion-input type="text" value="" [(ngModel)]="UserData.username"></ion-input>
</ion-item>
<ion-item>
<ion-label floating>Password</ion-label>
<ion-input type="password" [(ngModel)]="UserData.password"></ion-input>
</ion-item>
<button ion-button block class="margin-top" (click)="signup()">Signup</button>
Or Login
</ion-list>
</ion-content>

Related

How to save axios response to a variable and update the state in react js?

DataInsertSelect.jsx
import React from 'react'
import axios from 'axios'
import Data from './Data'
class DataInsertSelect extends React.Component {
constructor() {
super();
this.state = {
datam: ""
};
}
componentDidMount() {
const axios = require('axios');
// Make a request for a user with a given ID
axios.get('https://jsonplaceholder.typicode.com/todos?_limit=5')
.then( function (response) {
// handle success
console.log(response);
})
.catch(function (error) {
// handle error
console.log(error);
})
.then(function () {
// always executed
});
}
render() {
return (
<div className="container">
<div class="row">
<div className="col-sm-6 offset-sm-5">
{this.state.datam.map(joke=> (
<Data key={joke.id} title={joke.title} />
))}
</div>
</div>
<div className="col-sm-10 offset-sm-2"><br/>
<form class="form-inline" action=" " method="post" >
<div class="form-group">
<label for="email">FIRSTNAME:</label>
<input type="text" name="data1"
class="form-control" id="email" />
</div>
<div class="form-group">
<label for="pwd">LASTNAME:</label>
<input type="text" name="data2"
class="form-control" id="pwd" />
</div>
<button type="submit" class="btn btn-danger">Submit</button>
</form>
</div>
</div>
);
}
}
export default DataInsertSelect;
Data.jsx
import React from 'react'
class Data extends React.Component {
render() {
return (
<h3>{this.props.title}</h3>
);
}
}
export default Data;
Using console.log(response); outputs the 5 row data but I want to store the axios response to a variable and update the state and it needs to be displayed in the application.
Here also I am facing the error:
TypeError: this.state.datam.map is not a function.
I tried a lot but could not fix it. I am an absolute begginer at React and axios.
I hope this is clear . Thanks In advance.
The error :TypeError: this.state.datam.map is not a function you are getting because your this.state.datam is an empty string "", as declared in Constructor. And map is function available for Arrays and not string.
FIX: in constructor, instead use: this.state={ datam : [] }
To save the axios response into your state,do something like this:
axios.get('https://jsonplaceholder.typicode.com/todos?_limit=5')
.then((response)=> {
console.log(response);
this.setState({datam:response.data});
})
Hope this helps!
This example could help you
import React, { Component } from 'react'
import Axios from 'axios';
export default class example extends Component {
constructor(props) {
super(props)
this.state = {
users :[]
}
}
componentDidMount(){
Axios.get('http://127.0.0.1:8000/users/')
.then(function (response) {
this.setState({
users: response.data,
});
})
.catch(function (error) {
console.log(error);
})
};
render() {
return (
{
{ users.lenght >0 ? (
<tbody>
<tr>
<td>{this.state.id}</td>
<td>{this.state.username}</td>
<td>{this.state.email}</td>
</tr>
</tbody>
):(
<h1>No data available!</h1>
)
}
}
)
}
}

Material Dialog does not close

I have a material initiated by the navigation component if it matters, but the issue is I'm doing a login with the dialog and it does not seem to close when the login is successful and user is redirected to dashboard.html
import { Component, OnInit } from '#angular/core';
import {Observable} from 'rxjs';
import {map} from 'rxjs/operators';
import { AngularFireAuth } from "#angular/fire/auth";
import { AuthService } from "../../core/services/auth.service";
import { User, PrivateUser } from "../../core/models/user";
import { UserService } from "../../core/services/user.service";
import { MatDialog, MatDialogRef } from '#angular/material/dialog';
import { SignUpComponent } from '../../components/authentication/sign-up/sign-up.component';
import { SignInComponent } from '../../components/authentication/sign-in/sign-in.component';
#Component({
selector: 'app-dashboard',
templateUrl: './dashboard.component.html',
styleUrls: ['./dashboard.component.css']
})
export class DashboardComponent implements OnInit {
isLoggedIn$: Observable<boolean>;
isLoggedOut$: Observable<boolean>;
user$: Observable<any>;
constructor(
public afAuth: AngularFireAuth,
private authService: AuthService,
private userService: UserService,
public dialog: MatDialog,
public dialogRef: MatDialogRef<SignInComponent>
) { }
ngOnInit() {
this.dialogRef.close();
this.isLoggedIn$ = this.afAuth.authState.pipe(map(user => !!user));
this.isLoggedOut$ = this.isLoggedIn$.pipe(map(loggedIn => !loggedIn));
this.user$ = this.authService.user$;
}
here is my code in dashboard.ts
So it should trigger closing the dialog the moment it reaches the component page. but it's not closing the dialog and the dashboard couldn't be displayed. I'm not getting any console error as well so I'm not too sure what went wrong.
I've also tried calling this.dialog.closeAll; where it loads the dashboard but the dialog is still not being closed.
and this was where the SignInComponent was triggered
loginModal() {
this.dialog.open(SignInComponent)
};
}
on my html
<form [formGroup]="loginForm" (ngSubmit)="SignIn(email.value, password.value)">
<mat-form-field>
<input formControlName="email" name="email" matInput type="text" placeholder="email">
</mat-form-field>
<mat-form-field>
<input formControlName="password" name="password" matInput type="password" placeholder="Password">
</mat-form-field>
<div *ngIf="errorCode" class="notification is-danger">
Email and password does not match
</div>
<div class="d-flex flex-column align-items-center">
<button mat-raised-button class="button is-success w-100" type="submit" [disabled]="!loginForm.valid">
Continue
</button>
</div>
</form>
Add mat-dialog-close to your close button.
<mat-dialog-actions>
<button mat-button mat-dialog-close>No</button>
<!-- The mat-dialog-close directive optionally accepts a value as a result for the dialog. -->
<button mat-button [mat-dialog-close]="true">Yes</button>
</mat-dialog-actions>

Ionic 3 Insert Data to Firebase RTDB

this is my adduser.ts , can i insert 'email' without fill in adduser.html (use ngModel)? I got the value of email with this code : this.emailNow = fire.auth.currentUser.email;
import { Component } from '#angular/core';
import { IonicPage, NavController, NavParams } from 'ionic-angular';
import { User } from '../../model/user/user.model';
import { UserListService } from '../../services/user-list.service';
import * as firebase from 'firebase';
import { AngularFireAuth } from '#angular/fire/auth';
/**
* Generated class for the AdduserPage page.
*
* See https://ionicframework.com/docs/components/#navigation for more info on
* Ionic pages and navigation.
*/
#IonicPage()
#Component({
selector: 'page-adduser',
templateUrl: 'adduser.html',
})
export class AdduserPage {
user : User = {
name: '',
email: '',
address:'',
telp: '',
};
constructor(public navCtrl: NavController, private userListService: UserListService, public navParams: NavParams,
private fire: AngularFireAuth, public db: AngularFireDatabase) {
this.emailNow = fire.auth.currentUser.email;
}
ionViewDidLoad() {
console.log('ionViewDidLoad AdduserPage');
}
addUser(user : User) {
this.userListService.addUser(user);
then(ref => {
this.navCtrl.push('LoginPage');
})
}
}
and this is my adduser.html
<ion-header>
<ion-navbar>
<ion-title>Lengkapi Profil</ion-title>
</ion-navbar>
</ion-header>
<ion-content padding>
<ion-item>
<ion-label color="primary" stacked>Nama</ion-label>
<ion-input [(ngModel)]="user.name"></ion-input>
</ion-item>
<ion-item>
<ion-label color="primary" stacked>Alamat</ion-label>
<ion-input [(ngModel)]="user.address"></ion-input>
</ion-item>
<ion-item>
<ion-label color="primary" stacked>No Telp</ion-label>
<ion-input type="tel" [(ngModel)]="user.telp"></ion-input>
</ion-item>
<button ion-button (click)="addUser(user)">Lanjutkan</button>
</ion-content>
and this is user-list.service.ts
import { Injectable } from '#angular/core';
import { AngularFireDatabase } from 'angularfire2/database';
import { User } from '../model/user/user.model';
#Injectable()
export class UserListService {
private userListRef = this.db.list<User>('user-list');
constructor(private db: AngularFireDatabase) { }
getUserList() {
return this.userListRef;
}
addUser(user: User) {
return this.userListRef.push(user);
}
}
sorry for my english to bad.
You already retrieved the email here this.emailNow = fire.auth.currentUser.email;
then in the html do the following:
<ion-item>
<ion-label color="primary" stacked>Email</ion-label>
<ion-input type="email" readonly="true" [(ngModel)]="emailNow"></ion-input>
</ion-item>

How to pass selected (bootstrap card from homeComponent) to another component (favourtiesComponent) in angular 6?

EXAMPLE: When clicked on the Title of the Book(A Monk who sold his ferrari..), i want to pass the Selected One to the Favorites component.. by default it's adding all items in an array.
HomeComponent: (How to add single item on click than adding all items to the favourtiesComponent?)
FavourtiesComponent:
App Module
import { BrowserModule } from '#angular/platform-browser';
import { NgModule } from '#angular/core';
import { FormsModule } from '#angular/forms';
import { Routes, RouterModule } from '#angular/router';
import { AppComponent } from './app.component';
import { FormInputComponent } from './form-input/form-input.component';
import { MyBookService } from './mybook.service';
import { HeaderComponent } from './header/header.component';
import { FavsComponent } from './favs/favs.component';
import { HomeComponent } from './home/home.component';
import { FavouriteService } from './favs.service';
const appRoutes: Routes = [
{ path: '', redirectTo: '/home', pathMatch: 'full' },
{path: 'home', component: HomeComponent},
{path: 'favourites', component: FavsComponent},
];
#NgModule({
declarations: [
AppComponent,
FormInputComponent,
HeaderComponent,
FavsComponent,
HomeComponent
],
imports: [
BrowserModule,
FormsModule,
RouterModule.forRoot(appRoutes)
],
providers: [MyBookService, FavouriteService],
bootstrap: [AppComponent]
})
export class AppModule { }
HomeComponent.html
<!-- loading form -->
<app-form-input></app-form-input>
<!-- output -->
<div class="card text-white bg-dark" style="max-width: 18rem;"
*ngFor="let book of myBooks">
<div class="card-header" (click)="onAdd()"> {{ book.title }} </div>
<div class="card-body">
<!-- <h5 class="card-title">Dark card title</h5> -->
<p class="card-text"> {{ book.content }} </p>
</div>
</div>
HomeComponent.ts
import { Component, OnInit, Input, EventEmitter } from '#angular/core';
import { MyBookService } from '../mybook.service';
import { Book } from '../book.model';
#Component({
selector: 'app-home',
templateUrl: './home.component.html',
styleUrls: ['./home.component.css']
})
export class HomeComponent implements OnInit {
myBooks: Book[];
constructor(private bookService: MyBookService) {}
ngOnInit() {
this.myBooks = this.bookService.getBooks(); // Loading books
// Listening to changes
this.bookService.booksChanged
.subscribe(
(book: Book[]) => {
this.myBooks = book;
}
);
}
onAdd() {
this.bookService.addedBooks(this.myBooks);
}
}
MyBookService.ts
import { Book } from './book.model';
import { Subject } from 'rxjs';
import { FavouriteService } from './favs.service';
import { Injectable } from '#angular/core';
import { Favourites } from './favs/fav.model';
#Injectable()
export class MyBookService {
booksChanged = new Subject<Book[]>();
bookSelected = new Subject<Book>();
constructor(private favService: FavouriteService) {}
private myBooks: Book[] = [
new Book('A Monk who sold his ferrari', 'A burning sense of passion is the most potent fuel for your dreams.'),
new Book('The Secret', 'You are already incredibly blessed, you just haven’t noticed.')
];
getBooks() {
return this.myBooks.slice();
}
addBooks(book: Book) {
this.myBooks.push(book);
this.booksChanged.next(this.myBooks.slice());
}
addedBooks(favBook: Favourites[]) {
this.favService.addedFavBooks(favBook);
console.log('favBook: ', favBook);
}
}
FavourtiesService:
private favBooks: Favourites[] = [
];
getFavbooks() {
return this.favBooks.slice();
}
addedFavBooks(favBooks: Favourites[]) {
this.favBooks.push(...favBooks);
this.favBooksChanged.next(this.favBooks.slice());
}
}
FavouriteComponent.html
<div class="container mt-3">
<h3 class="mb-3">My Favourties: </h3>
</div>
<!-- output -->
<div class="card text-white bg-dark" style="max-width: 18rem;"
*ngFor="let favBook of favBooks">
<div class="card-header"> {{ favBook.title }} </div>
<div class="card-body">
<p class="card-text"> {{ favBook.content }} </p>
</div>
</div>
FavouritesComponent.ts
import { Component, OnInit } from '#angular/core';
import { FavouriteService } from '../favs.service';
import { Favourites } from './fav.model';
#Component({
selector: 'app-favs',
templateUrl: './favs.component.html',
styleUrls: ['./favs.component.css']
})
export class FavsComponent implements OnInit {
favBooks: Favourites[];
favBook: Favourites;
constructor(private favService: FavouriteService) {}
ngOnInit() {
// this.myBooks = this.bookService.addedBooks(); // Loading books
this.favBooks = this.favService.getFavbooks();
this.favService.favBooksChanged
.subscribe(
(favs: Favourites[]) => {
this.favBooks = favs;
}
);
}
}
Whenever a book is selected, add only that book to service. To do that, from HTML you can pass currently selected book to component.ts as given below.
After the modifications, The code might look like this.
HomeComponent.html
<!-- loading form -->
<app-form-input></app-form-input>
<!-- output -->
<div class="card text-white bg-dark" style="max-width: 18rem;"
*ngFor="let book of myBooks">
<div class="card-header" (click)="onAdd(book)"> {{ book.title }} </div>
<div class="card-body">
<!-- <h5 class="card-title">Dark card title</h5> -->
<p class="card-text"> {{ book.content }} </p>
</div>
</div>
HomeComponent.ts
import { Component, OnInit, Input, EventEmitter } from '#angular/core';
import { MyBookService } from '../mybook.service';
import { Book } from '../book.model';
#Component({
selector: 'app-home',
templateUrl: './home.component.html',
styleUrls: ['./home.component.css']
})
export class HomeComponent implements OnInit {
myBooks: Book[];
constructor(private bookService: MyBookService) {}
ngOnInit() {
this.myBooks = this.bookService.getBooks(); // Loading books
// Listening to changes
this.bookService.booksChanged
.subscribe(
(book: Book[]) => {
this.myBooks = book;
}
);
}
onAdd(selectedBook) {
this.bookService.addedBooks([selectedBook]);
}
}

undefined 'touched' in angular2

I am trying to validate my form but getting the error undefined touched, think i did something wrong with the import filed,can some one please suggest me some help.
My Template,
<h3 class = "head">MY PROFILE</h3>
<form [formGroup]="form" (ngSubmit)="onSubmit(form.value)">
<div class="row">
<div class="form-group">
<label class="formHeading">firstname</label>
<input type="text" id="facebook" class="form-control" [formControl]="form.controls['firstname']" >
</div>
<div *ngIf ="firstname.touched">
<div *ngIf ="!firstname.valid" class = "alert alert-danger">
<strong>First name is required</strong>
</div>
</div>
</div>
</form>
My Component,
import {Component} from '#angular/core';
import {Http, Response, Headers} from '#angular/http';
import {Observable} from 'rxjs/Observable';
import {Subject } from 'rxjs/Subject';
import {CORE_DIRECTIVES} from '#angular/common';
import { Router, ROUTER_DIRECTIVES } from '#angular/router';
import {Control,ControlGroup} from '#angular/common';
import {FORM_DIRECTIVES,FormBuilder,FormGroup,Validators, REACTIVE_FORM_DIRECTIVES} from '#angular/forms';
Component({
templateUrl: './components/profile/profile.html',
directives: [CORE_DIRECTIVES,ROUTER_DIRECTIVES,FORM_DIRECTIVES,REACTIVE_FORM_DIRECTIVES],
})
export class Profile {
http: Http;
form: FormGroup;
constructor(fbld: FormBuilder,http: Http,public router: Router) {
this.http = http;
this.form = fbld .group({
firstname: ['', Validators.required],
lastname: ['', Validators.required],
profilename :['', Validators.required],
image : [''],
phone : ['']
});
}
onSubmit(form) {
console.log(form);
var headers = new Headers();
headers.append('Content-Type','application/x-www-form-urlencoded');
this.http.post('http://localhost/angular/index.php/profile/addprofile', JSON.stringify(form),{headers:headers});
subscribe(response => {
if(response.json().error_code === 0) {
alert('added successfully');
this.router.navigate(['/demo/professional']);
} else {
alert('fail');
}
});
}
}
I am trying to validate my form but getting the error undefined touched, think I did something wrong with the import files. Can someone please suggest me some help.
You have to find the control in the form. Try to use:
form.controls['firstname'].touched
and:
form.controls['firstname'].valid

Resources