Ionic2 + Meteor:获取新插入项目的_id [英] Ionic2 + Meteor: get _id of new inserted item
问题描述
我正在使用 Meteor 数据库进行 Ionic2 项目。当我向数据库插入一个新项目时,我需要获取新生成的 _id
。但我不知道如何以正确的方式访问 Observable
。
I'm in an Ionic2 project with a Meteor Database. When I insert a new Item to the database I would need to get the newly generated _id
. But I don't know how to access the Observable
in the right way.
服务器:
newItem() {
if (!this.userId)
throw new Meteor.Error('unauthorized', 'User must be logged-in to insert an item');
return Items.insert({ ownerId: this.userId, title: 'New Item' });
}
客户:
MeteorObservable.call('newItem').subscribe({
next: () => {
//get _id
},
error: (e: Error) => {
console.log("Error: " + e);
}
});
Items.insert
返回 Observable< string>
包含插入的 _id
。如何在客户端的中访问
? _id
下一个:()
Items.insert
returns an Observable<string>
containing the inserted _id
. How can I access this _id
within next: ()
on client side?
修改:
我也在客户端尝试过此操作:
I also tried this on the client:
MeteorObservable.call('newItem').subscribe(
data => {
console.log(data);
},
error => {
console.log("Error: " + error);
}
);
但是数据
是一个空对象对象{_isScalar:false}
。 :(
But there data
is an empty Object Object {_isScalar: false}
. :(
Edit2:
我也在这里发布: https://forums.meteor.com/t/get--id-after- insert-from-returned-observable-on-client / 32106
推荐答案
错误发生在服务器端:
Items.collection.insert(...)
添加 .collection
后,它将返回新的 _id
-string。
With the added .collection
it's returning the new _id
-string.
这篇关于Ionic2 + Meteor:获取新插入项目的_id的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!