我正在使用AngularFire2,只是無法獲得查詢部分。 「limitToLast:1」正在返回所有內容,而不僅僅是最近的條目。我需要一種方法來按順序編號我的工作,我想獲得最新的作業ID(4),所以我可以添加+ 1,使下一個5limitToLast:1個查詢返回所有內容
job: FirebaseObjectObservable<any>;
test: any;
constructor(af: AngularFire) {
this.job = af.database.object('/jobs/', {
query:{ limitToLast: 1 }
});
};
createNewJob(){
this.job.subscribe(snap => {
this.test = snap;
});
}
相反的輸出我的單身最近進入我得到的一切:
[ null,
{ "customer": "Heather D", "description": "Work needs to be done" },
{ "customer": "Quick G", "description": "Unclog toilet" },
{ "customer": "Justin S", "description": "Do everything for everyone" }
{ "customer": "Jeff F", "description": "Ikea furniture" }
]
HTML
<button (click)="createNewJob()"> Create </button>
{{ test | json }}
這裏是我的結構
ROOT
jobs
1
customer: "Heather D"
description: "Work needs to be done"
2
customer: "Quick G"
description: "Unclog toilet"
3
customer: "Justin S"
description: "Do everything for everyone"
4
customer: "Jeff F"
description: "Ikea furniture"
更新1:
仍然有同樣的問題,但我覺得這是一個正確的方向邁出的一步。如我錯了請糾正我。我已經給我的工作現在一個唯一的ID,這似乎是多餘的給我,但新的結構是這樣的:
ROOT
jobs
1
customer: "Heather D"
description: "Work needs to be done"
id: 1
2
customer: "Quick G"
description: "Unclog toilet"
id: 2
3
customer: "Justin S"
description: "Do everything for everyone"
id: 3
4
customer: "Jeff F"
description: "Ikea furniture"
id: 4
,然後我改變了我的查詢:
query:{ orderByChild: 'id', limitToLast: 1 }
同樣的結果,仍不限於只有一份工作。
哦,我會嘗試列表,讓你知道!至於push(),我想用這個,但我仍然需要我的工作(工作編號:7051354)可以推送嗎?你是說我根本不應該使用交易,或者交易是我唯一的選擇嗎? – Jus10
它的工作!我得到了一個結果!最後!大聲笑。現在我對結果有一個小副作用。查詢:{orderByChild:'id',limitToFirst:1}給了我第三個結果。不是首先或最後。我改變它查詢:{limitToLast:1},它的工作....但....任何人都知道爲什麼會給我在我的列表中的結果? – Jus10