undefined 'touched' in angular2 - angularjs

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

Related

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>

Problem with React "Cannot read property 'addEventListener' of null"

Trying to add the login function to the login button but for some reason it says that "Cannot read property 'addEventListener' of null" and I have no idea how to fix it.
I have added the window.onload = function(){} which worked on my other component but doesn't work on this one.
Problem is in this part.
window.onload = function(){
document.getElementById("submit").addEventListener("click", function(){
UserLogin();
})
}
and here is the whole code:
import React from 'react';
import './App.css';
import * as firebase from 'firebase/app';
import 'firebase/auth';
import firebaseConfig from './Config';
//import { BrowserRouter as Router, Route, Link } from "react-router-dom";
firebase.initializeApp(firebaseConfig);
function UserLogin(){
var email = document.getElementById('email').value;
var password = document.getElementById('password').value;
firebase.auth().signInWithEmailAndPassword(email, password).catch(function(error) {
//var errorCode = error.code;
//var errorMessage = error.message;
});
}
window.onload = function(){
document.getElementById("submit").addEventListener("click", function(){
UserLogin();
})
}
function Login(){
return(
<div class="start">
<h1>Login</h1>
<div class="input">
<input id="email" type="text" placeholder="email"></input>
<input id="password" type="password" placeholder="password"></input>
<button id="submit">Login</button>
</div>
</div>
)
}
export default Login;
Login is likely not rendered yet when window.onload is called.
Why not just use the onClick property on the button?
import React from 'react';
import './App.css';
import * as firebase from 'firebase/app';
import 'firebase/auth';
import firebaseConfig from './Config';
//import { BrowserRouter as Router, Route, Link } from "react-router-dom";
firebase.initializeApp(firebaseConfig);
function UserLogin(){
var email = document.getElementById('email').value;
var password = document.getElementById('password').value;
firebase.auth().signInWithEmailAndPassword(email, password).catch(function(error) {
//var errorCode = error.code;
//var errorMessage = error.message;
});
}
function Login(){
return(
<div class="start">
<h1>Login</h1>
<div class="input">
<input id="email" type="text" placeholder="email"></input>
<input id="password" type="password" placeholder="password"></input>
<button id="submit" onClick={UserLogin}>Login</button>
</div>
</div>
)
}
export default Login;
If you want your UserLogin function should get called automatically when your component finishes loading, then you can make use of Hooks (Added in React 16.8).
import React, {useEffect} from 'react';
function Login(){
useEffect(() => {
UserLogin();
});
return(
<div class="start">
<h1>Login</h1>
<div class="input">
<input id="email" type="text" placeholder="email"></input>
<input id="password" type="password" placeholder="password"></input>
<button id="submit">Login</button>
</div>
</div>
)
}

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]);
}
}

var transporter is causing a webpack error and I have no clue why. I have the latest version of nodemailer

import React, { Component } from 'react'
import { render } from 'react-dom'
import { BrowserRouter, Route, Link, history } from 'react-router-dom'
import Footer from "./Footer"
import Menu from './Menu'
import {Helmet} from 'react-helmet'
import Scroll from 'react-scroll'
import Validation from 'react-validation'
import classNames from 'classnames'
import PropTypes from 'prop-types'
import { ajax } from 'jquery'
import nodemailer from 'nodemailer'
import smtpTransport from 'nodemailer-smtp-transport'
export default class Form extends Component {
var transporter = nodemailer.createTransport({
service: 'Gmail',
auth: {
user: 'gmail.user#gmail.com',
pass: 'userpass'
}
});
constructor(props) {
super(props);
this.state = {
Form: {
Name: "",
Email: "",
Website: "",
Subject: "",
Message: ""
}
}
this.onFormChange = this.onFormChange.bind(this)
}
onFormChange(e) {
var formName = this._name.value
var formEmail = this._email.value
var formWebsite = this._website.value
var formSubject = this._subject.value
var formMessage = this._message.value
console.log(formEmail);
this.setState({
Form: {
Name: formName,
Email: formEmail,
Website: formWebsite,
Subject: formSubject,
Message: formMessage
}
})
}
onFormSubmit(e){
e.preventDefault();
}
render() {
const formInputs = this.state.Form
return(
<section>
<Validation.components.Form
method="post"
onChange={this.onFormChange}>
<input
ref={input => this._name = input}
name="Name"
type="text"
placeholder="NAME" />
<input
ref={input => this._email = input}
name="Email"
type="text"
placeholder="EMAIL" />
<input
ref={input => this._website = input}
name="Website"
type="text"
placeholder="WEBSITE" />
<input
ref={input => this._subject = input}
name="Website"
type="text"
placeholder="SUBJECT" />
<textarea
ref={input => this._message = input}
name="Message"
type="text"
placeholder="MESSAGE" />
<input onSubmit={this.onFormSubmit.bind(this)} type="submit"/>
</Validation.components.Form>
<p>{this.state.Name}</p>
</section>
)
}
}
Here is my package.json
"nodemailer": "^4.1.0",
"nodemailer-smtp-transport": "^2.7.4",
And here is the webpack error im getting.
ERROR in ./js/Form.js
Module build failed: SyntaxError: Unexpected token (26:6)
24 | export default class Form extends Component {
25 |
> 26 | var transporter = nodemailer.createTransport({
| ^
27 | service: 'Gmail',
28 | auth: {
29 | user: 'gmail.user#gmail.com',
I dont understand why this declared variable would cause this error. I followed the nodemailer docs and have looked and looked but cant seem to find the reason this is happening. Been stick on this issue for a week. I need help. Thank you

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

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>

Resources