使用accounts-ui程序包时,流星中是否存在post createUser钩子? [英] Is there a post createUser hook in meteor when using accounts-ui package?

查看:60
本文介绍了使用accounts-ui程序包时,流星中是否存在post createUser钩子?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我有一个待办事项应用程序,并且我想确保每个注册用户都至少有一个待办事项开始,例如首次待办事项要删除!",我该如何在流星中做到这一点?

Let's say I have a todo app, and I want to make sure that every user that registers has at least one todo to start with, something like "First todo to cross off!", how would I do that in meteor?

一般来说,我的看法是,第一次创建用户时可以这样做(理想情况),或者每次登录时检查是否需要新的任务(不太理想).在后一种情况下,我可以检查Todos.findOne(),如果计数为0,则加1.但是,似乎无论是在页面加载时还是在路由器上还是在某些模板的.rendered函数上执行此操作时,我要检查的集合都尚未加载,所以我总是创建一个新的待办事项,即使一个真正的待办事项也是如此.确实存在.因此,如果有人可以解释如何解决这个问题,那就太好了.

In general, the way I see it, I can do it when the user is created for the first time (ideal), or check to see whether they need a new todo every time they log in (less ideal). In the latter case, I can do a check for Todos.findOne(), and if the count is 0, add one. However, seems that whether I do this in my router when the page loads, or on some template's .rendered function, the collection I'm checking hasn't been loaded yet, so I always create a new todo, even if one really does exist. So it'd be great if someone could explain how to get around that.

但是,我理想中想要的是能够在创建用户时创建新的Todo.有一个Accounts.onCreateUser方法,但是该方法用于将其他信息添加到用户配置文件,而不是创建后的钩子.还有一种方法可以使用Accounts.createNewUser通过回调以编程方式创建用户,但是我使用account-ui软件包,因此不以编程方式添加用户.在不太理想的情况下,无论何时用户登录,我都可以检查Todo,但是即使在那种情况下,似乎也有联合的Accounts.loginWithXService登录名,因此无论是否有用户登录,都不确定如何处理回调服务类型.

But, what I'd ideally want is the ability to just create a new Todo when the user is created. There is a Accounts.onCreateUser method, but that is used to add additional info to user profile, not a post-create hook. There's also a method to programmatically create the user using Accounts.createNewUser with a callback, but I'm using the accounts-ui package so am not programmatically adding users. In a less ideal case, I could check for the Todo whenever the user logs in, but even in that case, there seems to be a federated Accounts.loginWithXService login, so not sure how to handle the callback when any user logs in, regardless of service type.

我想我一定错过了一些简单的东西,如果这很明显,那么我深表歉意.感谢您的帮助.

I think I must be missing something simple, so apologies if this is super obvious. Any help is appreciated.

推荐答案

我使用了上面介绍的_.wrap方法,但希望包含其他建议.从新的自定义回调中调用原始回调是一个好主意.流星在回调上做了一些我们不想错过的事情.

I used the _.wrap method described above but wanted to include an additional suggestion. It's a good idea to call the original callback from your new custom callback. Meteor does some things on the callback that we don't want to miss.

修改后的代码对我来说就像冠军:

Modified code that worked like a champ for me:

Accounts.createUser = _.wrap(Accounts.createUser, function(createUser) {

    // Store the original arguments
    var args = _.toArray(arguments).slice(1),
        user = args[0];
        origCallback = args[1];

    var newCallback = function(error) {
        // do my stuff

        origCallback.call(this, error);
    };

    createUser(user, newCallback);
});

这篇关于使用accounts-ui程序包时,流星中是否存在post createUser钩子?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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