gpt4 book ai didi

javascript - 错误: Property 'getChildren' does not exist on type 'DataSnapshot'

转载 作者:行者123 更新时间:2023-12-01 01:49:28 26 4
gpt4 key购买 nike

...文档中确实如此 P:

我在文件顶部import * as firebase from "firebase";。 uid 获取功能有效。由于某种原因,它不喜欢 for 循环的语法...

我还尝试了语法:for (DataSnapshot child :parent.getChildren()) { } 然后编译器告诉我 for 循环开始的行中需要有一个分号。

getMessages() {
return new Promise(function (resolve) {
return firebase.auth().onAuthStateChanged(function (user) {
if (user) {
resolve(user.uid);
}
});
}).then((result) => {
return firebase.database().ref('mailboxes/' + result).once('value').then((snapshot) => {
let messageArray;
for (let snap of snapshot.getChildren()) {
messageArray.push(snap.val());
console.log('snapshot key:' + snap.key);
console.log('snapshot val:' + snap.val());
};
return messageArray;
});
});
}

最佳答案

您需要使用订阅来观察更改。使用 AngularFire 监视他们何时登录并获取 UID(假设您在 Firebase 中使用身份验证登录,以便使用 UID 作为树路径保存所有数据

import { AngularFirestore } from 'angularfire2/firestore';
import { AngularFireDatabase, AngularFireList } from 'angularfire2/database';
import { AngularFireAuth } from 'angularfire2/auth';
import { switchMap, map } from 'rxjs/operators';
import { Observable, pipe } from 'rxjs';
import { Observable, Subscription } from 'rxjs';
import firebase as firebase from 'firebase/app';

private myOAuthSubscription: Subscription;
private myDatasubscription: Subscription;
public userloggedin:boolean = false;
public uid:string = '';

public this.items:any = [];

constructor(
public _DB: AngularFireDatabase,
public _afAuth: AngularFireAuth,
) {


try {
this.myOAuthSubscription = this._afAuth.authState.subscribe(user => {

if (user && user.uid) {

console.log('loggedin = true');
this.userloggedin = true;
this.uid = String(user.uid);

this.funDoDB():

} else {

console.log('loggedin = false');
this.userloggedin = true;
this.uid = '';

}
});
} catch (e) {
console.error("fbData_subscription", e);
}



}

ngOnDestroy() {
this.myOAuthSubscription.unsubscribe();
this.myDatasubscription.unsubscribe();
}


private funDoDB(){
if(this.userloggedin == true){
try {

//subscription using AngulaFire
this.myDatasubscription = this._DB.list('mailboxes/' + this.uid).snapshotChanges().pipe(map(actions => {
return actions.map(action => ({ key: action.key, val: action.payload.val() }));
}))
.subscribe(items => {

this.items = [];
this.items = items.map(item => item);

console.log("db results",this.items);

var icount=0;

for (let i in this.items) {

console.log("key",this.items[i].key);
console.log("val",this.items[i].val);
console.log("----------------------------------);

//checking if something exists
if (this.items[i].key == 'SomeNodePath') {
var log = this.items[i].val;
}

}


} catch (e) {
console.error(e);
}


});
}
}

npm install --save angularfire2 firebase
npm install -D rxjs@6.2.2 rxjs-compat@6.2.2

关于javascript - 错误: Property 'getChildren' does not exist on type 'DataSnapshot' ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51667824/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com