Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

How to create a new document and add data to it in angular? #3030

Answered by hakimio
Sapython asked this question in Q&A
Discussion options

I tried this

import { Injectable } from '@angular/core';
import { doc, enableIndexedDbPersistence, getFirestore, provideFirestore } from '@angular/fire/firestore';
import { setDoc } from '@firebase/firestore';
@Injectable({
 providedIn: 'root'
})
export class UserDataService {
 firestore:any;
 constructor() { 
 this.firestore = provideFirestore(() => {
 this.firestore = getFirestore();
 enableIndexedDbPersistence(this.firestore);
 return this.firestore;
 });
 }
 addNewUser(): void {
 console.log('addNewUser',this.firestore);
 const userDoc = doc(this.firestore, 'users/user1');
 console.log(userDoc);
 setDoc(userDoc, {'Alpha':'beta'}).then(() => {
 console.log('Document successfully written!');
 }).catch(() => {
 console.log('Error writing document');
 });
 }
}

but it doesn't work I know angular/fire 7 is new and docs are not ready but I really want to know how it works. I know I am doing something wrong here.
Please provide an easy solution to this.

You must be logged in to vote
  1. You have to provide Firestore in app module and then inject it in your service.
  2. provideFirestore() returns ModuleWithProviders<FirestoreModule> not Firestore instance. You are misusing it.

Replies: 1 comment 2 replies

Comment options

  1. You have to provide Firestore in app module and then inject it in your service.
  2. provideFirestore() returns ModuleWithProviders<FirestoreModule> not Firestore instance. You are misusing it.
You must be logged in to vote
2 replies
Comment options

Can you show an example of how to do it?

Comment options

I linked to the example in my answer.

Answer selected by Sapython
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants

AltStyle によって変換されたページ (->オリジナル) /