【问题标题】:Ionic2 + Meteor: get _id of new inserted itemIonic2 + Meteor:获取新插入项目的_id
【发布时间】:2016-12-09 06:01:39
【问题描述】:

我在一个带有 Meteor 数据库的 Ionic2 项目中。当我向数据库中插入一个新项目时,我需要获取新生成的_id。但我不知道如何以正确的方式访问Observable

服务器:

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 返回一个包含插入的_idObservable<string>。如何在客户端的next: () 中访问此_id

编辑: 我也在客户端上试过这个:

MeteorObservable.call('newItem').subscribe(
  data => {
    console.log(data);
  },
  error => {
    console.log("Error: " + error);
  }
);

但是data 是一个空对象Object {_isScalar: false}。 :(

编辑2: 我也发在这里了:https://forums.meteor.com/t/get--id-after-insert-from-returned-observable-on-client/32106

【问题讨论】:

  • 这是来自 Meteor 的 next: () => {} 吗?还是离子的?
  • 通常你会打电话给.subscribe(data => { console.log("id: " + data); }, error => { console.log("Error: " + error); }});
  • next:来自他们的教程:angular-meteor.com/tutorials/whatsapp2/ionic/chats-mutations 我已经尝试过与您的代码类似的东西,但我会再试一次
  • 代码中的data 是一个空的Object {_isScalar: false} :(

标签: mongodb meteor ionic-framework ionic2


【解决方案1】:

错误出现在服务器端:

Items.collection.insert(...)

添加.collection 后,它会返回新的_id-string。

【讨论】:

  • 为什么需要使用原始集合?您应该也可以在不收集的情况下使用。
  • 没有集合就不会返回_id。
猜你喜欢
  • 1970-01-01
  • 2014-09-08
  • 1970-01-01
  • 2016-01-24
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-09-17
相关资源
最近更新 更多