Tammibriggs
Tammibriggs

Reputation: 639

How can i change this Firebase version 8 code to version 9

How to do this with Firebase version 9 Web SDK

db.collection('rooms')
 .doc(id)
 .collection('messages')
 .orderBy('timestamp', 'asc')
 .onSnapshot((snapshot) => 
   setState(snapshot.docs.map(doc => 
   doc.data()))
 )

Upvotes: 1

Views: 1692

Answers (2)

R...
R...

Reputation: 2570

related to your question indirectly, if you want to procrastinate and upgrade to 9 but leave your code as is, you can use the version 9 compat (Compatibility) link. Just replace your version 8 with version 9 and add "-compat" before ending '.js' .e.g.

<script src="https://www.gstatic.com/firebasejs/9.3.0/firebase-app-compat.js"></script>

this way don't change your code while using latest version of firebase. The disadvantage is that you won't benefit from module nature firebase 9.

Upvotes: 0

Renaud Tarnec
Renaud Tarnec

Reputation: 83103

The following should do the trick:

import { collection, query, orderBy, onSnapshot } from "firebase/firestore"; 

const id = ...;

const messagesColRef = collection(db, "rooms", id, "comments");
const messagesQuery = query(messagesColRef, orderBy("timestamp"));
onSnapshot(messagesQuery, (snapshot) => {
   setState(snapshot.docs.map(doc => 
   doc.data()))
});

Have a look at the Firestore reference.

Upvotes: 3

Related Questions