未捕获(承诺):错误:Firebase.update 失败:第一个参数



我正在尝试更新火碱节点,但我总是得到

未捕获(承诺(:错误:Firebase.update 失败:第一个参数

我认为问题可能是currCount输出为未定义,这很奇怪,因为我在应用程序的其他部分中使用相同的功能并且工作正常。

这是我的代码:


//With this I'm 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 which works so I'm not having any issue with this one but you might want to 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;
}
}

解决了!

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);
}

最新更新