我试图更新一个frebase节点,但我总是得到
未捕获(承诺):错误:Firebase.update失败:第一个参数
我认为问题可能是currCount输出为未定义,因为我在应用程序的其他部分使用相同的功能并且工作正常。
这是我的代码:
//With this Im trying to update a node adding +1
async addCountRegs(company: string): firebase.Promise<any>{
var currCount;
await this.authData.getCountRegs(company)
.then((countregs:any) => {
currCount = countregs.value;
});
console.log('currCount = ' + currCount);
var regsData = {
countregs: currCount + 1
};
var updates = {};
updates['/companies/' + company] = regsData;
return this.afDB.database.ref().update(updates);
}
//This is the function that should return the current value
async getCountRegs(company: string){
let firstRef = this.db.object(`companies/${company}`, {preserveSnapshot:true});
let promise = new Promise((resolve, reject) => {
firstRef.subscribe(snapshot => {
let a = snapshot.exists();
if(a) {
resolve(snapshot.child("countregs").val());
}
});
});
return promise;
}
//And this is the function wich works so Im not having any issue with this one but you might wanna check it out
async maxRegsReached(company: string): Promise<any> {
var count;
var max;
await this.getCountRegs(company)
.then((usersRef:any) => {
count = usersRef;
});
await this.getMaxRegs(company)
.then((usersRef:any) => {
max = usersRef;
});
console.log('count = ' + count);
console.log('max = ' + max);
if (count >= max){
console.log('true');
return true;
} else {
console.log('false');
return false;
}
}
&#13;
答案 0 :(得分:0)
解决!
async addCountRegs(company: string){
var currCount;
await this.authData.getCountRegs(company)
.then((countregs:any) => {
currCount = countregs;
});
console.log('currCount = ' + currCount);
let endpoint = this.afDB.object(`/companies/${company}/countregs`);
endpoint.set(currCount + 1);
}