将关联文档_id添加到Accounts.onCreateUser()

时间:2015-11-06 16:25:32

标签: function meteor user-accounts

我在Accounts.onCreateUser()中尝试将文档ID插入user.profile键,以便能够在签名时将不同集合中的单独文档(保存用户信息)关联到用户英寸

//serverMain.js
Accounts.onCreateUser(function(options,user){
  var userId = user._id;
  user.profile = user.profile || {};
  _.extend(user.profile, {profComp: false});
  insertShopObject(userId);
  return user;
});

我使用的插页是

insertShopObject(userId);

这会将带有预设字段的新文档插入到名为“ShopList”的单独集合中,我已将userId作为参数传递到“ShopList”集合中作为字段添加。我可以从服务器控制台看到,当我调用insertShopObject(userId)时会返回文档_id;

enter image description here

我想以某种方式在插入文档时捕获该id,并将其添加到用户创建的user.profile键中,如此

_.extend(user.profile,{shopId: <-- ?-->})

这是insertShopObject函数,我试过返回而不是控制台将'result'记录到一个没有运气的保持变量中。

   //serverMain.js

    insertShopObject = function(userId){
    var newShop = {
      //pre-set fields.....
      }
    ShopList.insert(newShop, function(error,result){
        if(error){console.log(error);}
        else {console.log(result)}
     });
}

1 个答案:

答案 0 :(得分:1)

您需要使插入同步才能生效。省略ShopList.insert()的回调并执行:

insertShopObject = function(userId){
  var newShop = {
    //pre-set fields.....
  }
  var shopId = ShopList.insert(newShop);
  return shopId;
}