我的问题可能不仅限于firebase数据库,但我的问题发生在我使用Firebase数据库的Angular项目上时,所以我也会标记Firebase和Angular。
根据Firebase Real Time Database documentation,大多数ThenableReference返回一个Promise。
当我编写将新对象推送到数据库或更新数据库中现有对象的基本功能时,它将返回一个Promise
,因此我可以使用then
来执行以下操作DB的操作。
出现错误时(例如,权限被拒绝),我可以像这样在屏幕上显示它:
但是我的问题是,我不知道如何将错误写入数据库以将其记录下来。
下面是我的代码:
this.clientService.createClient(this.client).then(data=>{ //when successfully Push object to DB
this.loggerService.log('success','createClient', this.client, {id:data['key'],msg:'Successfully Create the Client'}, 'add-client') //Log the result into DB
this.router.navigate(['search-results',data.key,'client-details']) //route user to another page
this.toastrService.success('','Successfully Create the Client') //display a successful Pop Up message
},error=>{
console.log(error) //this is working
let msg = error.toString()
this.loggerService.log('error','createClient', this.client, msg, 'add-client') //this is not working
this.toastrService.error(error,'Failed to create the Client') //this is working like the attached photo
});
下面是我的loggerService.log代码:
log(status,action,req,res:Object,site){
let object = {
dtcreate: Date.now(),
uid:this.currentUser.uid,
userEmail:this.currentUser.email,
action:action,
req:req,
res:res,
site:site,
status:status
}
return this.logRef.push(object)
}
已编辑:
下面是console.log(error)
的错误,这是我希望登录到数据库的错误(错误:PERMISSION_DENIED:权限被拒绝)。
Error: PERMISSION_DENIED: Permission denied
at vendor.js:88796
at exceptionGuard (vendor.js:76410)
at Repo.push../node_modules/@firebase/database/dist/index.cjs.js.Repo.callOnCompleteCallback (vendor.js:88787)
at vendor.js:88564
at vendor.js:87739
at PersistentConnection.push../node_modules/@firebase/database/dist/index.cjs.js.PersistentConnection.onDataMessage_ (vendor.js:87772)
at Connection.push../node_modules/@firebase/database/dist/index.cjs.js.Connection.onDataMessage_ (vendor.js:87057)
at Connection.push../node_modules/@firebase/database/dist/index.cjs.js.Connection.onPrimaryMessageReceived_ (vendor.js:87051)
at WebSocketConnection.onMessage (vendor.js:86952)
at WebSocketConnection.push../node_modules/@firebase/database/dist/index.cjs.js.WebSocketConnection.appendFrame_ (vendor.js:86557)
谢谢!