【发布时间】:2019-12-01 05:06:42
【问题描述】:
我正在尝试为我的 Angular 应用程序的所有操作实施审计跟踪,例如“删除”“添加用户”“更新”等。我能够使用“帖子”中的“发布博客”功能成功实现这一点-dashboard.component.ts”,现在我尝试对“student-user.component.ts”中的“删除用户”函数执行相同操作,但编译器返回错误“属性‘id’不存在于输入“无效””
我尝试将 id 定义为 'id: any;'和 'id =''' 但我认为这不是解决方案。错误不会消失。审计跟踪功能应该是在控制台打印输出(审计跟踪),并将审计跟踪日志存储在审计跟踪模型下的数据库中
这是我的 student-user.component.ts。留意 deleteStudent(value) 函数...这就是我要实现它的地方
import { Component, OnInit } from '@angular/core';
import { map, catchError} from 'rxjs/operators';
import {FirebaseService} from '../services/firebase.service';
import {Router} from '@angular/router';
import { AngularFirestore} from "angularfire2/firestore";
import { Observable } from 'rxjs';
import { AuditTrailService } from 'src/app/services/audit-trail/audit-trail.service';
import { AngularFireStorage } from '@angular/fire/storage';
@Component({
selector: 'app-student-user',
templateUrl: './student-user.component.html',
styleUrls: ['./student-user.component.scss']
})
export class StudentUserComponent implements OnInit{
students: any;
students_data :any;
public searchString: string;
response: any;
is_loading: any;
ideas; any;
term = '';
//id: any[];
constructor(
public firebaseService: FirebaseService,
private router: Router,
public db: AngularFirestore,
private storage: AngularFireStorage,
public _auditTrailService:AuditTrailService
) { }
ngOnInit() {
this.getStudents()
this.searchStudents(event)
}
getStudents() {
this.students_data = this.db.collection('User', ref => ref.where('role', '==', 'student')).snapshotChanges().pipe(map(changes => {
return changes.map(a => {
const data: any = a.payload.doc.data();
data.id = a.payload.doc.id;
return data;
});
})
);
this.students_data.subscribe(
res => {
console.log(res);
this.students = res;
//this.blogs_snapshot = res;
});
}
searchStudents(event){
if (event.key === "Enter"){
console.log(this.term)
this.db.collection('User', ref => ref.where('full_name', '==', this.term)).valueChanges().subscribe(
res => {
this.response = res;
console.log(this.response);
this.ideas = res;
this.is_loading = false;
}
);
}
}
deleteStudent(value){
this.db.collection('User').doc(value).delete().then(
res =>{
console.log(res)
this.students = res;
const _audit_trail = {
'action' : ' has deleted a student ',
'object': res.id,
'created_at': new Date(),
'user':{
'id': '4545454545454545454',
'email':'daanyu@gmail.com' }
}
console.log(_audit_trail)
this._auditTrailService.createAuditTrailLog(_audit_trail)
},
err=>{
console.log(err)
}
)
}
}
这是我的错误...
ERROR in src/app/student-user/student-user.component.ts(87,23): error TS2339: Property 'id' does not exist on type 'void'.
我使用 Firebase 作为我的数据库,这是我期望在我的审计跟踪模型下在数据库中返回的内容:
action“删除了一个学生” created_at 2019 年 7 月 22 日下午 5:49:33 UTC+3 用户: 电子邮件“daanyu@gmail.com” 编号“4545454545454545454”
【问题讨论】:
-
这是我在控制台 DocumentReference firestore 中所期望的:Firestore {_queue: AsyncQueue, INTERNAL: {...}, clientRunning: true, _config: FirestoreConfig, _databaseId: DatabaseId, ...} id: (.. .) parent: (...) path: (...) _firestoreClient: FirestoreClient {platform: BrowserPlatform, databaseInfo: DatabaseInfo, credentials: FirebaseCredentialsProvider, asyncQueue: AsyncQueue, clientId: "OB7KkI69MTqqYMsGetou", ...} _key: DocumentKey {path: ResourcePath} proto: 对象
-
当您创建
_audit_trail对象时,此问题出现在'object': res.id,这一行中。您能否将console.log(res)的回复添加到您的问题中。 -
嘿@nash11,这是我在控制台中的回复...当我尝试删除用户
core.js:15724 ERROR Error: Uncaught (in promise): TypeError: Cannot read property 'id' of undefined TypeError: Cannot read property 'id' of undefined你有什么建议?
标签: angular typescript firebase angular7 angularfire2