'(snap: DataSnapshot) => 类型的参数void' 不可分配给类型为 '(a: DataSnapshot) => 的参数布尔值' [英] Argument of type '(snap: DataSnapshot) => void' is not assignable to parameter of type '(a: DataSnapshot) => boolean'
问题描述
我已经阅读了有关此问题的几个问题和答案,但无法解决.
我正在使用 Ionic2,并且我有一种方法可以从 Firebase 数据库 v3 中检索数据.我不明白为什么我在执行 ionic serve
时会在控制台中出现以下错误:
错误 TS2345:类型参数'(快照:DataSnapshot)=>void' 不可分配给类型为 '(a: DataSnapshot) => 的参数布尔'.void"类型不能分配给boolean"类型.
这是方法:
构造函数(私有http:Http){firebase.database().ref('users').orderByChild("id").on("value", function(snapshot){让项目 = [];快照.forEach(快照=> {项目.推({uid: snap.val().uid,用户名:snap.val().username,});});});}}
DataSnapshot
中的 forEach
方法有这个签名:
forEach(action: (a: firebase.database.DataSnapshot) => boolean): boolean;
作为 action
可以返回 true
以短路枚举并提前返回.如果返回假值,则枚举继续正常进行.(这在文档中有所提及.)>
为了安抚 TypeScript 编译器,最简单的解决方案是返回 false
(继续枚举子快照):
数据库().ref("用户").orderByChild("id").on("value", (snapshot) => {让项目 = [];snapshot.forEach((snap) => {项目.推({uid: snap.val().uid,用户名:snap.val().username});返回假;});});
I've already read several questions and answers about this problem but wasn't able to solve it.
I'm using Ionic2 and I have a method which retrieves data from Firebase Database v3.
I don't understand why I get following error in console when I do ionic serve
:
Error TS2345: Argument of type '(snap: DataSnapshot) => void' is not assignable to parameter of type '(a: DataSnapshot) => boolean'.
Type 'void' is not assignable to type 'boolean'.
This is the method:
constructor(private http: Http) {
firebase.database().ref('users').orderByChild("id").on("value", function(snapshot){
let items = [];
snapshot.forEach(snap => {
items.push({
uid: snap.val().uid,
username: snap.val().username,
});
});
});
}
}
The forEach
method in the DataSnapshot
has this signature:
forEach(action: (a: firebase.database.DataSnapshot) => boolean): boolean;
as the action
can return true
to short-circuit the enumeration and return early. If a falsy value is returned, enumeration continues normally. (This is mentioned in the documentation.)
To appease the TypeScript compiler, the simplest solution would be to return false
(to continue enumerating the child snapshots):
database()
.ref("users")
.orderByChild("id")
.on("value", (snapshot) => {
let items = [];
snapshot.forEach((snap) => {
items.push({
uid: snap.val().uid,
username: snap.val().username
});
return false;
});
});
这篇关于'(snap: DataSnapshot) => 类型的参数void' 不可分配给类型为 '(a: DataSnapshot) => 的参数布尔值'的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!