最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Firebase 9 - how to chain 'addDoc' or similar to a 'collection'? - Stack Overflow

programmeradmin1浏览0评论

Previously in Firebase you could add a document like this:

const myNewDoc = await db.collection('some-collection-name').add({ //Document details here... });

With the introduction of Firebase 9 this no longer works.

Instead of .add I think I am supposed to use an imported .addDoc method.

But it seems I cannot chain .addDoc onto .collection.

If I try to do something like this:

const myNewDoc = await db.collection('some-collection-name').addDoc({ //Document details here... });

TypeScript throws this error:

Property 'addDoc' does not exist on type 'CollectionReference<DocumentData>'.ts(2339)

I could create something more verbose like this:

const someCol = collection(db, "some-collection-name");
const newDoc = await addDoc(someCol, {
    //Document details here...
});

But I would rather "chain" it like before.

Is that possible? How would it be done?

And should I even be using .addDoc? Or something else?

Previously in Firebase you could add a document like this:

const myNewDoc = await db.collection('some-collection-name').add({ //Document details here... });

With the introduction of Firebase 9 this no longer works.

Instead of .add I think I am supposed to use an imported .addDoc method.

But it seems I cannot chain .addDoc onto .collection.

If I try to do something like this:

const myNewDoc = await db.collection('some-collection-name').addDoc({ //Document details here... });

TypeScript throws this error:

Property 'addDoc' does not exist on type 'CollectionReference<DocumentData>'.ts(2339)

I could create something more verbose like this:

const someCol = collection(db, "some-collection-name");
const newDoc = await addDoc(someCol, {
    //Document details here...
});

But I would rather "chain" it like before.

Is that possible? How would it be done?

And should I even be using .addDoc? Or something else?

Share Improve this question edited Nov 16, 2021 at 4:40 Dharmaraj 51.1k8 gold badges67 silver badges98 bronze badges asked Nov 15, 2021 at 23:04 TinyTigerTinyTiger 2,19112 gold badges66 silver badges131 bronze badges 0
Add a ment  | 

2 Answers 2

Reset to default 6

The addDoc() is a top level function in Modular SDK. Try refactoring your code like this:

import { collection, addDoc } from "firebase/firestore"; 

const newDoc = await addDoc(collection(db, "some-collection-name"), {
  // Document Data
});
console.log("Document written with ID: ", newDoc.id);

The documentation has examples of both name-spaced and the new syntax.


If you are using pat version to use older syntax then you would have to use add() itself.

import { Firestore, collection, collectionData, addDoc } from '@angular/fire/firestore';

firestore: Firestore = inject(Firestore);

addDoc(collection( this.firestore, 'some-collection-name'),Document details here
)
发布评论

评论列表(0)

  1. 暂无评论