Reputation: 12925
There is an ionic 2 project.
Here is my code:
export class PostPage {
posts : [any];
constructor(private nav:NavController,
private postSvc: PostService) {
}
ngOnInit() {
this.postSvc.load()
.subscribe( data => {
this.posts = data;
});
}
The error message is for this.posts = data. I defined this.posts as [any]. Also in my PostService I defined the data as [] too.
Code for PostService:
load() {
return new Observable( observer => {
var Post = Parse.Object.extend("Post");
var query = new Parse.Query(Post);
query.find({
success: results => {
var data = [];
for (var i = 0; i < results.length; i++) {
var object = results[i];
var item = this.createPost(object);
data.push(item);
}
observer.next(data);
observer.complete();
},
error: error => {
observer.error(error.message);
}
});
});
}
I got the error message:
Type '{}' is not assignable to type 'any[]
As you see, I call observer.next(data) where the data is []. I don't understand why my ionic 2 think my data is a '{}' . Any idea? Thanks.
Upvotes: 3
Views: 10864
Reputation: 275927
For off posts : [any];
you probably meant posts : any[];
Type '{}' is not assignable to type 'any[]
This can happen if the TypeScript compiler cannot figure out the type by type inference e.g. if the inferred type is Promise<{}>
the value passed to then
will be of type {}
.
Specify the type in:
return new Observable( observer => {
e.g:
return new Observable<any>( observer => {
Upvotes: 8