Meteor collection.insert回调返回新的id [英] Meteor collection.insert callback to return new id

查看:63
本文介绍了Meteor collection.insert回调返回新的id的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在meteor.collection.insert的回调中获取新插入的docuement的id。

I want to get the id of a newly inserted docuement in the callback of meteor.collection.insert.

我按如下方式插入Doc:

I insert the Doc as follows:

Meteor.call('createDoc', {
    key1: value1,
    key2: value2
})

createDoc函数如下所示:

The createDoc function looks like that:

Meteor.methods createDoc: (options) ->
    incidents.insert
        key1: options.value1
        key2: options.value2
        , callback(error, result)

callback = (error,result) ->
    console.log result

文档说:

callback Function
Optional. If present, called with an error object as the first argument and,
if no error,the _id as the second.

所以我希望结果返回新的id,但是我得到一个引用错误,说错误和结果没有定义。我在这里弄错了什么?任何帮助都非常值得赞赏。

So I expect result to return the new id, but am getting a Reference Error saying that error and result are not defined. What am I getting wrong here? Any help is much apprecated.

推荐答案

BenjaminRH对于更容易,更可能的方法是正确的。但是,有时您需要服务器来完成工作,和/或某些人坚持认为这是在流星中进行数据库工作的唯一方法,这就是您的代码如何做到这一点:

BenjaminRH is right about the easier, more likely way to do this. However, there are times when you need the server to do the work, and/or some who insist that is the only way to do database work even in meteor, and here's how your code would do that:

# server code
Meteor.methods createDoc: (options) ->
  created = incidents.insert
    key1: options.value1
    key2: options.value2
  created

# on client code

Meteor.call 'createDoc', info, (err, data) ->
  if err
    console.log JSON.stringify err,null,2
    # DO SOMETHING BETTER!!
  else
    Session.set('added doc', data )  
    # and something reactive in waiting for session to have 'added doc'

这篇关于Meteor collection.insert回调返回新的id的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆