Hi,
I have trouble to deposit data to database in firebase in Ionic 4, I tried to
create a module storage with a provider StoreToFirebaseService to save data to firebase.
I did the following:
npm install firebase angularfire2
- add config to app/environments/environment.ts
export const environment = {
production: false,
firebase: {
apiKey: ‘’,
authDomain: ‘’,
databaseURL: ‘’,
projectId: ‘’,
storageBucket: ‘’,
messagingSenderId: ‘’,
appId: “”
}
};
- storage.module.ts
import { AngularFireModule } from ‘angularfire2’;
import { environment } from ‘…/…/environments/environment’;
import { AngularFirestoreModule } from ‘angularfire2/firestore’;
@NgModule({
declarations: ,
imports: [
CommonModule,
AngularFireModule.initializeApp(environment.firebaseConfig),
AngularFirestoreModule
],
providers: [ StoreToFirebaseService ]
})
export class StorageModule { }
- store-to-firebase.service.ts
…
import { AngularFirestore } from ‘angularfire2/firestore’;
…
export class StoreToFirebaseService {
constructor(
public afs: AngularFirestore
) {
}
addSurvey(){
console.log("Inside addSurvey!");
return new Promise<any>((resolve, reject) => {
this.afs.collection('/result').add({
ID: "Q2",
Result1: false,
Result2: false
})
.then(
(res) => {
resolve(res)
},
err => reject(err)
)
})
}
}
- In the component, I imported the service
import { StoreToFirebaseService } from ‘…/…/storage/store-to-firebase.service’;
export class InitiatedDrinkComponent implements OnInit {
constructor(
private storeToFirebaseService: StoreToFirebaseService
) { }
storeData(){
console.log(“Inside storeData”);
this.storeToFirebaseService.addSurvey();
}
}
- Fore firebase database, I start in test mode with an empty database.
But “result” is not created with the fields ID, Result1 and Result2, any suggestion? Thanks!