I am facing an issue with my household app where, upon clicking the button to navigate to the addtodo
page, the URL specific to the user's house is getting lost. This results in the todolist
being stored as undefined
on Firebase instead of under the chosen house. Can anyone offer insights into why this might be happening? I have attempted passing the URL in the following manner.
Todolist.ts
import { Component, OnInit } from '@angular/core';
import {NavController} from "@ionic/angular";
import { House, HouseService } from '../Services/house.service';
import { ActivatedRoute, Route, Router } from '@angular/router';
import { TodoserviceService } from '../Services/todoservice.service';
import { AngularFirestore } from '@angular/fire/firestore';
import { map } from 'rxjs/operators';
import { Todolist } from '../modal/Todolist';
import { Observable } from 'rxjs';
import { UserService } from '../Services/user.service';
@Component({
selector: 'app-todolist',
templateUrl: './todolist.page.html',
styleUrls: ['./todolist.page.scss'],
})
export class TodolistPage implements OnInit {
minDate = new Date().toISOString();
house;
currentHouse:House;
currentHouseId: string;
DB;
todoArray = [];
todo = {
title: '',
description: '',
last_Date: new Date().toISOString(),
}
constructor(private afs: AngularFirestore,
private router: Router,
private houseService: HouseService,
private userService: UserService,private navCtrl: NavController) {
//get ID of house from URL
let id1 = this.router.url.split('id=');
let id2 = id1[1].toString();
let id3 = id2.split('/');
let id = id3[0].toString();
this.houseService.getHouse(id);
if (id) {
this.houseService.getHouse(id).subscribe(house => {
this.currentHouse = house;
this.house = id;
});}
//initialize DB
this.DB = this.afs.collection('house').doc(id).collection('todolist');
this.DB.snapshotChanges().subscribe(colSnap => {
this.todoArray = [];
colSnap.forEach(snap => {
let todo: any = snap.payload.doc.data();
todo.id = snap.payload.doc.id;
todo.last_Date = new Date(todo.last_Date).toDateString();
this.todoArray.push(todo);
});
});
}//end constructor
ngOnInit() {
}
gotoAddToDo(house:string){
this.navCtrl.navigateForward(['/add-todo',{id:house}]);
}
}
add-todo.ts
import { Component, OnInit } from '@angular/core';
import { NavController } from '@ionic/angular';
import { ToastController } from '@ionic/angular';
import { House, HouseService } from '../Services/house.service';
import { ActivatedRoute, Route, Router } from '@angular/router';
import { TodoserviceService } from '../Services/todoservice.service';
import { AngularFirestore } from '@angular/fire/firestore';
import { map } from 'rxjs/operators';
import { Todolist } from '../modal/Todolist';
import { Observable } from 'rxjs';
import { UserService } from '../Services/user.service';
@Component({
selector: 'app-add-todo',
templateUrl: './add-todo.page.html',
styleUrls: ['./add-todo.page.scss'],
})
export class AddTodoPage implements OnInit {last_Date = new Date().toISOString();
currentHouse;
house;
currentHouseId;
DB;
todoArray = [];
todo = {
title: '',
description: '',
last_Date: new Date().toISOString(),
}
constructor(private afs: AngularFirestore,
private router: Router,
private houseService: HouseService,
private userService: UserService) {
let id1 = this.router.url.split('id=');
let id2 = id1[1].toString();
let id3 = id2.split('/');
let id = id3[0].toString();
this.houseService.getHouse(id);
if (id) {
this.houseService.getHouse(id).subscribe(house => {
this.currentHouse = house;
this.house = id;
});}
//initialize DB
this.DB = this.afs.collection('house').doc(id).collection('todolist');
this.DB.snapshotChanges().subscribe(colSnap => {
this.todoArray = [];
colSnap.forEach(snap => {
let todo: any = snap.payload.doc.data();
todo.id = snap.payload.doc.id;
todo.last_Date = new Date(todo.last_Date).toDateString();
this.todoArray.push(todo);
});
});
}//end constructor
ngOnInit() {
}
addtodo(){
this.DB.add(this.todo);
this.resetTodo();
console.log("todoadded")
}
//function to reset values of bill object
resetTodo(){
this.todo = {
title: '',
description: '',
last_Date: new Date().toISOString(),
}
}
}