将关联文档 _id 添加到 Accounts.onCreateUser() 中的 user.profile 键

Adding associative document _id to user.profile key in Accounts.onCreateUser()

我试图在 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

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

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

这是 insertShopObject 函数,我试过返回而不是控制台将“结果”记录到一个保持变量中,但没有成功。

   //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)}
     });
}

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

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