Reputation: 421
I'm trying to update Firebase data, and I'm running into the error "Property 'update' does not exist on type 'subscription'. I can console log the object, and it is indeed the object I want. I'm just not sure how I can update the object using the "subscribe" method on the observable.
import { Injectable } from '@angular/core';
import { Post } from './models/post.model';
import { AngularFireDatabase, FirebaseListObservable } from 'angularfire2/database';
@Injectable()
export class PostService {
posts: FirebaseListObservable<any[]>;
constructor(private database: AngularFireDatabase) {
this.posts = database.list('posts');
}
getPosts(){
return this.posts;
}
getPostById(postId: string){
return this.database.object('posts/' + postId)
}
updatePost(localPost){
var postInFirebase = this.getPostById(localPost.$key).subscribe(dataLastEmittedFromObserver => {
postInFirebase = dataLastEmittedFromObserver
console.log(postInFirebase)
postInFirebase.update({content: localPost.content});
});
};
deletePost(localPostToDelete){
let postInFireBase = this.getPostById(localPostToDelete.$key);
postInFireBase.remove();
}
addPost(newPost: Post){
this.posts.push(newPost)
}
}
Upvotes: 0
Views: 37
Reputation: 6365
Try removing the var postInFirebase =
from the subscribing line, because Observer.subscribe
returns a Subscription
object synchronously before the asynchronous callback is called. This will probably cause the TypeScript compiler to assign the type Subscription
to your variable postInFirebase
and throws a compilation error.
updatePost(localPost){
this.getPostById(localPost.$key).subscribe(dataLastEmittedFromObserver => {
let postInFirebase = dataLastEmittedFromObserver
console.log(postInFirebase)
postInFirebase.update({content: localPost.content});
});
}
Or even better
updatePost(localPost){
this.getPostById(localPost.$key).subscribe(postInFirebase => {
postInFirebase.update({content: localPost.content});
});
}
You seem confused by the asynchronous signature of Observables. When you call var value = Observable.subscribe((data) => { ...});
then value
will be a Subscription and NOT the observed data, you can only access the observable data INSIDE the asynchronous callback. This makes me think that your deletePost
method also won't work.
Upvotes: 1