此函數記錄用戶讀取頁面並在用戶點擊下一頁時觸發。檢查數組中是否有任何項目等於此值
它首創訂閱到數據庫,檢索所有用戶已經閱讀網頁的清單..
我的目標是隻記錄頁面,用戶尚未讀取例如不在數據庫中。
如果從數據庫返回的列表包含與剛纔讀取的頁面相同的頁碼,則此函數不會將頁面記錄爲正在讀取。
但是在第一次點擊這隻作品,然後,如果我回到同一頁面再次,然後單擊下一步它會記錄的頁碼到數據庫...
如何修復這個bug任何建議,需要再次注意這個代碼我知道它簡單的事情..乾杯!
logAsWatched() {
let uid = this.windowRef.nativeWindow.firebase.auth().currentUser.uid;
this.projectId = this.uidpage.project.id;
this.subscription = this.af.database.list(`pagelogs/${uid}/${this.projectId}`).subscribe(
data => {
console.dir(data);
if (data.length >= 1) {
let pagelogArray = [];
for (let i = 0; i < data.length; ++i) {
let pageNumber = data[i];
pagelogArray.push(pageNumber);
}
console.log('pagelogArray', pagelogArray);
console.log('page order', this.pageOrder);
let newArray = [];
for (let i = 0; i < pagelogArray.length; ++i) {
newArray.push(pagelogArray[i].pNumber);
}
console.log(`newArray : ${newArray}`);
if (!newArray.indexOf(this.pageOrder)) {
console.log('page has allready been logged');
} else {
console.log('logging this page as watched');
this.setLogAsWatchedData();
}
} else {
console.log('No page logs have been added');
this.setLogAsWatchedData();
}
}, (error) => { console.log(`page log error: ${error}`); }
);
}
setLogAsWatchedData() {
this.subscription.unsubscribe();
let uid = this.windowRef.nativeWindow.firebase.auth().currentUser.uid;
this.windowRef.nativeWindow
.firebase.database().
ref(`pagelogs/${uid}/${this.projectId}`)
.push().set({ pNumber: this.pageOrder });
}
您錯誤地檢查了頁面被記錄:'if(!newArray.indexOf(this.pageOrder))'。它應該是'if(newArray.indexOf(this.pageOrder)!== -1)' –