Meteor collection.insert 回调问题 [英] Meteor collection.insert callback issues

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

问题描述

根据 Meteor 文档....

According to the Meteor documentation....

collection.insert(doc, [callback])

回调函数

可选.如果存在,调用时将错误对象作为第一个参数,_id 作为第二个参数.

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

...然后再下来...

...then later down...

在服务器上,如果您不提供回调,则插入块直到数据库确认写入,或者在出现问题时抛出异常.如果您确实提供了回调,则插入立即返回.插入完成(或失败)后,将使用错误和结果参数调用回调,与方法相同.

On the server, if you don't provide a callback, then insert blocks until the database acknowledges the write, or throws an exception if something went wrong. If you do provide a callback, insert returns immediately. Once the insert completes (or fails), the callback is called with error and result arguments, same as for methods.

它是错误和 _id 还是错误和结果?我确实有 Meteor.methods 正确触发回调,但有错误,结果可用于范围.

Which is it, error and _id or error and result? I do have Meteor.methods that are firing their callbacks correctly with error, result available to the scope.

我只是无法让回调在 collection.insert(doc, [callback]) 上正常工作

I just can't get the callback to work correctly on a collection.insert(doc, [callback])

无论如何我都无法通过回调来注册任何东西?

Either way I can't get my callback to register anything?

function insertPost(args) {
  this.unblock;
  if(args) { 
    post_text = args.text.slice(0,140);
    var ts = Date.now();  
    Posts.insert({
      post: post_text,
      created: ts
    }, function(error, _id){
      // or try function(error, result) and still get nothing 
      // console.log('result: ' + result);
      console.log('error: ' + error);
      console.log('_id: ' + _id); //this._id doesn't work either
    });

  }
  return;
}

我做错了什么?我从凌晨 2 点开始编码......下午 6 点我的时区......我很模糊,所以我可能(可能)遗漏了一些很明显的东西.

What am I doing wrong? I have been up since 2 am coding...6 pm my time zone...I am blurry, so I might (probably) be missing something quite obvious.

干杯史蒂夫

推荐答案

这是一个错误,已在下一个版本中修复.现在,如果您向 insert 提供回调,它将使用 errorresult 参数调用,其中 result是新文档的 ID,如果有错误,则为 null.

This was a bug, fixed in the next release. Now, if you provide a callback to insert, it will be called with error and result arguments, where result is the ID of the new document, or null if there's an error.

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

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