我正在使用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