I can not insert a record into firestore database using angular 12 - css

There is no error showing when i run ng serve or ng build, but it can't still make a record. I am blown away. Please help me.
I can show you some relevant parts in which i made some changes.
i created a student class in model.ts file, then i created an instance of the class in service class. Afterward, in build component of html file i created the submit() method as shown below
model.ts
export class Students{
id!: string;
matricule!:string;
nom!: string;
prenom!: string;
Quartier!:string;
age!:number
}
student.service.ts
import { AngularFirestoreDocument } from '#angular/fire/compat/firestore';
import { Students } from './model';
#Injectable({
providedIn: 'root'
})
export class StudentService {
constructor() { }
formData: Students = new Students();
}
student.component.ts
import { AngularFirestore } from '#angular/fire/compat/firestore';
import { NgForm } from '#angular/forms';
import { StudentService } from 'src/app/shared/student.service';
#Component({
selector: 'app-student',
templateUrl: './student.component.html',
styleUrls: ['./student.component.scss']
})
export class StudentComponent implements OnInit {
constructor(public service:StudentService, private firestore:AngularFirestore)
{ }
ngOnInit(): void {
this.resetForm();
}
resetForm(form?:NgForm) {
if(form!= null) {
form.resetForm();
this.service.formData = {
id:'',
matricule:'',
nom:'',
prenom:'',
age:0,
Quartier:''
}
}
}
onSubmit(form:NgForm) {
let data = form.value;
this.firestore.collection('students').add(data);
this.resetForm(form);
}
}
student.component.html
<form #form="ngForm" autocomplete="off" (submit)="onSubmit(form)">
<div class="form-group">
<input name="matricule" #matricule="ngModel" [(ngModel)]="service.formData.matricule" class="form-control" placeholder="Matricule" required>
<div *ngIf="matricule.invalid && matricule.touched" class="validation-error">Ce chant est obligatoire</div>
</div>
<div class="form-group">
<input name="nom" #nom="ngModel" [(ngModel)]="service.formData.nom" class="form-control" placeholder="Nom" required>
<div *ngIf="nom.invalid && nom.touched" class="validation-error">Ce chant est obligatoire</div>
</div>
<div class="form-group">
<input name="prenom" #prenom="ngModel" [(ngModel)]="service.formData.prenom" class="form-control" placeholder="Prenom" required>
<div *ngIf="prenom.invalid && prenom.touched" class="validation-error">Ce chant est obligatoire</div>
</div>
<div class="form-group">
<input type="number" name="age" #age="ngModel" [(ngModel)]="service.formData.age" class="form-control" placeholder="Age" required>
<div *ngIf="age.invalid && age.touched" class="validation-error">Ce chant est obligatoire</div>
</div>
<div class="form-group">
<input name="Quartier" #Quartier="ngModel" [(ngModel)]="service.formData.Quartier" class="form-control" placeholder="Quartier" required>
<div *ngIf="Quartier.invalid && Quartier.touched" class="validation-error">Ce chant est obligatoire</div>
</div>
<div class="form-group">
<button type="submit" [disabled]="form.invalid" class="btn btn-lg btn-block btn-info">SUBMIT</button>
</div>
</form>

Related

how coul i throw validations to a composable function vue 3

I am using vuelidate to validate my forms but seeing that the code is repetitive I wanted to make a composable function that validates a data object and the rules, but when I use the same ass function to compare the passwords, I get the following error
UserForm.vue:124 Uncaught (in promise) ReferenceError: Cannot access 'form' before initialization
certainly form is started after its invocation, I can't think how to send the rules to validate the passwords or any other field in the future.
this my vue 3 form component
<template>
<form #submit.prevent="submitForm" class="w-full">
<!-- nombre de usuario y nickname -->
<div class="main-box">
<div class="input-box">
<label class="label-item" for="grid-nick-name">
nombre de usuario
</label>
<input :class="[ !v$.nickname.$error ?'input-item' : 'input-item-error']" id="grid-nick-name" type="text" placeholder="user"
v-model="form.nickname">
<p class="error-msg" v-if="v$.nickname.$error">Porfavor rellena el campo</p>
</div>
<div class="input-box">
<label class="label-item" for="grid-name">
nombre
</label>
<input :class="[ !v$.name.$error ?'input-item' : 'input-item-error']" id="grid-name" type="text" placeholder="name lastname"
v-model="form.name">
<p class="error-msg" v-if="v$.name.$error">Porfavor rellena el campo</p>
</div>
</div>
<!-- document -->
<div class="main-box">
<div class="input-box-full">
<label class="label-item" for="grid-document">
Documento
</label>
<input :class="[ !v$.document.$error ?'input-item' : 'input-item-error']" id="grid-document" type="text" placeholder="00000000"
v-model="form.document">
<p class="error-msg" v-if="v$.document.$error">Campo vacio / datos invalidos</p>
</div>
</div>
<!-- password y password re -->
<div class="main-box">
<div class="input-box">
<label class="label-item" for="grid-password">
contraseña
</label>
<input :class="[ !v$.password.$error ?'input-item' : 'input-item-error']" id="grid-password" type="password" placeholder="*********"
v-model="form.password">
<p class="error-msg" v-if="v$.password.$error">Porfavor rellena el campo</p>
</div>
<div class="input-box">
<label class="label-item" for="grid-re-password">
repetir contraseña
</label>
<input :class="[ !v$.repassword.$error ?'input-item' : 'input-item-error']" id="grid-re-password" type="password" placeholder="*********"
v-model="form.repassword">
<p class="error-msg" v-if="v$.repassword.$error">Las Contraseñas deben coincidir</p>
</div>
</div>
<div class="main-box">
<div class="input-box">
<label class="label-item" for="grid-entity">
Entidad
</label>
<div class="relative">
<select :class="[ !v$.entity.$error ?'input-item' : 'input-item-error']" id="grid-entity" v-model="form.entity">
<option v-bind:value="'Hardware'">Hardware</option>
<option>Software</option>
<option>Red</option>
</select>
<div class="icon-item">
<svg class="fill-current h-4 w-4" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 20 20"><path d="M9.293 12.95l.707.707L15.657 8l-1.414-1.414L10 10.828 5.757 6.586 4.343 8z"/></svg>
</div>
</div>
</div>
<div class="input-box">
<label class="label-item" for="grid-position">
Cargo
</label>
<div class="relative">
<select :class="[ !v$.position.$error ?'input-item' : 'input-item-error']" id="grid-position" v-model="form.position">
<option selected>Baja</option>
<option>Media</option>
<option>Alta</option>
</select>
<div class="icon-item">
<svg class="fill-current h-4 w-4" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 20 20"><path d="M9.293 12.95l.707.707L15.657 8l-1.414-1.414L10 10.828 5.757 6.586 4.343 8z"/></svg>
</div>
</div>
</div>
</div>
<div>
<button type="submit"
class="submit-button">Guardar</button>
</div>
</form>
</template>
and this is the script vue 3 form component
<script setup>
import { required, numeric, alphaNum, sameAs, minLength } from '#vuelidate/validators';
import { useFormValidator } from "#/composables/useFormValidator.js";
const initialState = {
nickname:'',
name:'',
document:'',
password:'',
repassword:'',
entity:'',
position:'',
}
const validations = {
nickname: { required },
name: { required, alphaNum },
document: { required, numeric },
password: { required, minLength: minLength(6) },
repassword: { required, samePassword: sameAs(form.password)},
entity: { required },
position: { required },
}
const {
form,
v$,
submitForm
} = useFormValidator(initialState,validations);
</script>
here's my composable function validation
import { reactive } from '#vue/reactivity';
import useVuelidate from '#vuelidate/core';
import { computed, inject } from 'vue';
export const useFormValidator = (initialValue,validations) => {
const swal = inject('$swal');
const form = reactive({...initialValue});
const rules = computed(() => {
return {
...validations
}
});
const v$ = useVuelidate(rules,form);
const submitForm = async () => {
const isvalidForm = await v$.value.$validate();
if(!isvalidForm) return ;
else{
//aler succes
swal("Guardado!","el usuario a sido guardado correctamente","success");
//object initial state
Object.assign(form,initialValue);
//validations reset form
v$.value.$reset();
}
}
return {
form,
v$,
submitForm,
}
}
i need to passt diferent validation rules every time i use the composable

How to display Http Response response in HTML using Angular

I want to display my object values in HTML tags but nothing comes up. The object appears successfully in the console. When I simply place res only [object object] displays on the page and res.json and res.text display nothing. Please see code below.
Shopping-Cart-Child.component.html
<form #form="ngForm" autocomplete="off">
<div class="form-group">
<div class="input-group">
<div class="input-groupprepend">
<div class="input-group-text bg-white">
<i class="fas fa-user-tie"></i>
</div>
</div>
<input placeholder="Person Number" type="text" [(ngModel)]=personNo name="personNo">
</div>
</div>
<br>
<div class="form-group">
<button class="btn btn-success btn-lg btn-block" (click)=SearchCustomer()>Get shopping cart</button>
</div>
<div class="form-group">
<div class="row">
<div class="col-md-7">
<p>{{results}}</p>
</div>
</div>
</div>
</form>
Shopping-cart-child.Component.ts
import { Component, OnInit } from '#angular/core';
import {HttpClient,HttpResponse} from '#angular/common/http';
#Component({
selector: 'app-shopping-cart-child',
templateUrl: './shopping-cart-child.component.html',
styles: [
]
})
export class ShoppingCartChildComponent implements OnInit {
personNo=''
results:any;
constructor(private http:HttpClient) { }
SearchCustomer(){
return this.http.get("http://lab.tekchoice.co.za/api/v1/CCWeb/GetShoppingCart?accountNo="+this.personNo)
.subscribe((res:Response)=>{
console.log(res);
this.results = res.json;
}
)
}
ngOnInit(): void {
}
}
Use the json pipe.
E.g.
<p>{{results | json}}</p>

Can't validate email with Vuelidate in Vue3

I'm trying to use validation option in a form. The app is developing under Vue3.
I have installed npm install #vuelidate/core #vuelidate/validator into project folder. In a file main.js I have been trying to add Vuelidate as following:
import { createApp } from 'vue'
import Vuelidate from 'vuelidate'
import App from './App.vue'
import './registerServiceWorker'
import router from './router'
import store from './store'
import 'materialize-css/dist/js/materialize.min'
createApp(App).use(store).use(router).use(Vuelidate).mount('#app')
Next I am working on Login.vue file as following
<template>
<form class="card auth-card" #submit.prevent="onSubmit">
<div class="card-content">
<span class="card-title">Example</span>
<div class="input-field">
<input
id="email"
type="text"
v-model.trim="email"
:class="{invalid: ($v.email.$dirty && !$v.email.required) || ($v.email.$dirty && !$v.email.email)}"
>
<label for="email">Email</label>
<small class="helper-text invalid"
v-if="$v.email.$dirty && !$v.email.required"
>Could not be empty</small>
<small class="helper-text invalid"
v-else-if="$v.email.$dirty && !$v.email.email"
>Incorrect form</small>
</div>
</div>
<div class="card-action">
<div>
<button
class="btn waves-effect waves-light auth-submit"
type="submit">
Enter-login
<i class="material-icons right">send</i>
</button>
</div>
</div>
</form>
</template>
<script>
import { email, required, minLength } from '#vuelidate/validators'
import useVuelidate from '#vuelidate/core'
export default {
name: 'login',
setup () {
return { v$: useVuelidate() }
},
data: () => ({
email: '',
password: ''
}),
validations: {
email: { email, required },
password: { required, minLength: minLength(6) }
},
methods: {
onSubmit () {
if (this.$v.$invalid) {
this.$v.$touch()
return
}
this.$router.push('/')
}
}
}
</script>
Then I try to run all that with npm run serve but with no success. Chrome DeveloperTools inform me about "Uncaught (in promise) TypeError: Cannot read property 'super' of undefined".
What did I do wrong? Is it possible to use Vue3 and Vuelidate together?
Lidia
Step 1: Import useVuelidate inside component
import useVuelidate from "#vuelidate/core";
import { email, required, minLength } from "#vuelidate/validators";
Step 2: Initalize useVuelidate inside component
setup() {
return { v$: useVuelidate() };
},
Step 3: Initalize your modal data
data() {
return {
email: '',
password: ''
};
},
Step 4: Add validations rule
validations() {
return {
email: { email, required },
password: { required, minLength: minLength(6) }
};
},
Step 5: Add form submit method
methods: {
onSubmit: function() {
this.v$.$touch();
if (this.v$.$error) return;
alert('Form is valid')
}
}
Step 6: HTML template design will be like,
<form class="card auth-card" #submit.prevent="onSubmit">
<div class="card-content">
<span class="card-title">Example</span>
<div class="input-field">
<label for="email">Email <span class="required">*</span></label>
<input id="email" type="text" v-model.trim="email">
<small class="error" v-for="(error, index) of v$.email.$errors" :key="index">
{{ capitalizeFirstLetter(error.$property) }} {{error.$message}}
</small>
</div>
<div class="input-field">
<label for="email">Password <span class="required">*</span></label>
<input id="email" type="text" v-model.trim="password">
<small class="error" v-for="(error, index) of v$.password.$errors" :key="index">
{{ capitalizeFirstLetter(error.$property) }} {{error.$message}}
</small>
</div>
</div>
<div class="card-action">
<div>
<button class="btn waves-effect waves-light auth-submit" type="submit"> Enter-login<i class="material-icons right">send</i>
</button>
</div>
</div>
</form>
</template>
DEMO

Angular Object is possibly 'null'. Error at <span *ngIf="checkoutForm.get('emailAddr').hasError('required')">Enter email address</span>

Hi I am new to Angular 11 can anyone please help me with this error -
This is my code.
checkout.component.html
<h3>Checkout forms - Using Reactive forms</h3>
<div class="container">
<form [formGroup]="checkoutForm" (ngSubmit)="postData();">
<div class="form-group">
<label for="exampleInputEmail1">Email address</label>
<input type="email" class="form-control" id="exampleInputEmail1" aria-describedby="emailHelp" placeholder="Enter email" name="emailAddressField" formControlName="emailAddr">
<span *ngIf="checkoutForm.get('emailAddr').hasError('required')">Enter email address</span>
</div>
<div class="form-group">
<label for="exampleInputPassword1">Quantity</label>
<input type="password" class="form-control" id="exampleInputPassword1" placeholder="Password" name="passwordField" formControlName="quantity">
<small id="emailHelp" class="form-text text-muted">Enter Quantity</small>
</div>
<div class="form-check">
<input type="checkbox" class="form-check-input" id="exampleCheck1" name="termsField" formControlName="terms">
<label class="form-check-label" for="exampleCheck1">Agree to Terms</label>
</div>
<br>
<button type="submit" class="btn btn-primary" [disabled]="!checkoutForm.valid">Checkout</button>
</form>
</div>
I am getting error as Error at <span *ngIf="checkoutForm.get('emailAddr').hasError('required')">Enter email address
checkout.component.ts
import { Component, Input, OnInit } from '#angular/core';
import { FormGroup, FormControl, FormBuilder,Validators} from '#angular/forms';
#Component({
selector: 'app-checkout',
templateUrl: './checkout.component.html',
styleUrls: ['./checkout.component.css']
})
export class CheckoutComponent implements OnInit {
checkoutForm:FormGroup;
constructor(private formBuilder:FormBuilder) {
this.checkoutForm = formBuilder.group({
emailAddr: ['',Validators.required],
quantity: ['',Validators.required],
terms: ['',Validators.requiredTrue]
});
}
ngOnInit(): void {
}
postData(){
console.log(this.checkoutForm);
}
}
Instead of using
<span *ngIf="checkoutForm.get('emailAddr').hasError('required')">Enter email address</span>
Try
<span *ngIf="checkoutForm.get('emailAddr')?.hasError('required')">Enter email address</span>
i have tested this code on my computer maybe it will also help you to resolve your compilation error.
You can use *ngIf="cadastroForm.get('username')?.errors">.
You have to use the ?.

Angular firebase upload images

I am a beginner working with Angular 5 and firebase.
As practice I decided to create a blog.
My problem is that I can not upload my images in firestore. When I upload to Cloud storage and try to retrieve the download link, it does not register in firestore.
Take a look at my code and give me suggestions.
import { Component, OnInit } from '#angular/core';
import { AuthService } from 'src/app/shared/services/auth.service';
import { finalize } from 'rxjs/operators';
import { Observable } from 'rxjs';
import { AngularFireStorage } from '#angular/fire/storage';
import { PostsService } from 'src/app/shared/services/posts.service';
import { ToastrService } from 'ngx-toastr';
import { Router } from '#angular/router';
#Component({
selector: 'app-posts',
templateUrl: './posts.component.html',
styleUrls: ['./posts.component.scss']
})
export class PostsComponent implements OnInit {
uploadPercent: Observable<number>;
downloadURL: Observable<string>;
title: string;
content: string;
description: string;
image: string = null;
constructor(private auth: AuthService,
private toast: ToastrService,
private postService: PostsService,
private store: AngularFireStorage,
private router: Router) { }
ngOnInit() {
}
createPost() {
// tslint:disable-next-line:prefer-const
let data = {
content: this.content,
image: this.image,
description: this.description,
publication: new Date(),
title: this.title
};
this.router.navigate(['/admin']);
this.postService.create(data);
this.title = '';
this.description = '';
this.content = '';
this.toast.success('Votre article a été publié');
}
uploadImage(event) {
// tslint:disable-next-line:prefer-const
let file = event.target.files[0];
// tslint:disable-next-line:prefer-const
let path = `posts/${file.name}`;
if (file.type.split('/')[0] !== 'image') {
return alert('Erreur, ce fichier n\'est pas une image');
} else {
// tslint:disable-next-line:prefer-const
let ref = this.store.ref(path);
// tslint:disable-next-line:prefer-const
let task = this.store.upload(path, file);
this.uploadPercent = task.percentageChanges();
console.log('Image chargée avec succès');
task.snapshotChanges().pipe(
finalize(() => {
this.downloadURL = ref.getDownloadURL();
this.downloadURL.subscribe(url => {
console.log(url);
});
}
)
).subscribe();
}
}
}
<div class="col-8">
<div class="input-group mb-3">
<div class="custom-file">
<input type="file" class="custom-file-input" id="inputGroupFile03" aria-describedby="inputGroupFileAddon03" (change)="uploadImage($event)" accept=".png,.jpg" [(ngModel)]="image">
<label class="custom-file-label" for="inputGroupFile03">Choisir une image</label>
</div>
</div>
<div class="progress" [hidden]="!uploadPercent">
<mat-progress-bar mode="determinate" value="{{ uploadPercent | async }}"></mat-progress-bar>
{{ uploadPercent | async }}
</div>
<div [hidden]="!image">
<img [src]="image || '//:0'">
</div>
<div class="form-group">
<label for="title"> Titre du post </label>
<input type="text" class="form-control" name="title" id="title" placeholder="Titre" required [(ngModel)]="title"/>
</div>
<div class="form-group">
<label for="description"> Description </label>
<input type="text" class="form-control" name="description" id="description" placeholder="Desciption"
data-toggle="tooltip" data-placement="top" title="Ajouter une breve description de l'article" required [(ngModel)]="description"/>
</div>
<div class="form-group">
<label for="content"> Contenu de l'artcle </label>
<textarea class="form-control" name="content" id="content" cols="15" rows="5" placeholder="..." required [(ngModel)]="content"></textarea>
</div>
<button class="btn btn-success btn-lg btn-block" (click)="createPost()">Poster l'article <i class="fa fa-paper-plane"
aria-hidden="true"></i>
</button>
</div>
enter image description here

Resources