skip to Main Content

I see typeOf error on default keyword. Here are my imports:

import { AngularFirestore } from '@angular/fire/compat/firestore';
import { map } from 'rxjs/operators';
import firebase from 'firebase/compat/app'; 
import 'firebase/compat/firestore';

export class PostsService {

  constructor(private afs: AngularFirestore) { }
countViews(postId: any){
    const viewCount= {
      views: firebase.default.firestore.FieldValue.increment(1) //only issue is with default
    }
    this.afs.doc(`posts/${postId}`).update(viewCount).then(() => {
      console.log("VIEWS updated"); //this works
      
    })
  }
}

Dependencies in package.json

  "dependencies": {
    "@angular/animations": "^14.2.0",
    "@angular/common": "^14.2.0",
    "@angular/compiler": "^14.2.0",
    "@angular/core": "^14.2.0",
    "@angular/fire": "^7.6.1",
    "@angular/forms": "^14.2.0",
    "@angular/platform-browser": "^14.2.0",
    "@angular/platform-browser-dynamic": "^14.2.0",
    "@angular/router": "^14.2.0",
    "bootstrap": "4.6",
    "firebase-tools": "^12.5.2",
    "rxjs": "~7.5.0",
    "tslib": "^2.3.0",
    "zone.js": "~0.11.4"
  }

The following should capture the current value on no. of views on a post.

views: firebase.default.firestore.FieldValue.increment(1)

I updated my import statemnts as shown earlier. I did see a similar question posted on stackoverflow, but the answers don’t seem to be working for me.

2

Answers


  1. Use Named Imports:
    You’re using firebase as a default import, but TypeScript might not be recognizing it properly. To fix this, you can try the following:

    import { firestore } from 'firebase/compat/app';
    import 'firebase/compat/firestore';
    

    Type Assertion:
    If you prefer to keep the default import, you can use a type assertion to let TypeScript know that firebase.default is an object that includes Firestore. Here’s how you can do it:

    const viewCount = {
      views: (firebase.default.firestore as any).FieldValue.increment(1)
    };
    
    Login or Signup to reply.
  2. firebase does not have default property. Just use firebase.firestore.FieldValue.increment. And also you should imprt firebase/compat.

    import firebase from 'firebase/compat';
    
    :
    
      views: firebase.firestore.FieldValue.increment(1)
    
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search