1

我尝试在autoform插入到另一个集合(Meteor.users)后插入到用户配置文件数组中。流星Autoform,集合钩子 - 集合插入后如何插入用户配置文件数组?

我的简单模式阵列设置这样的 -

listings: { 
type: [String], 
optional: true 
}, 
"listings.$.id": { 
type: String, 
optional: true 
} 

(轮廓模式中),这是我收集挂机方法应该上市插入后插入。

//Add listing to user collection on submit 
Listings.after.insert(function(userId, doc) { 
console.log("STUFF"); 
Meteor.users.update({_id : userId}, 
{ 
    $push : 
    { 
     'profile.listings.$.id' : this._id 
    } 
} 

在我看来,这应该工作。表单正确插入没有集合挂钩,但现在当我提交表单,我得到这个错误在我的JS控制台:

错误:筛选出不在模式中的键后,您的修改器现在为空(...)

console.log(“stuff”)触发器,我在控制台中发现错误之前。

任何人有任何想法如何做到这一点?

编辑 - 固定的几件事情通过它切换到:

Listings.after.insert(function(userId, doc) { 
console.log("STUFF" + userId + '  ' + this._id); 
Meteor.users.update({_id: userId }, 
{ 
    $set : 
    { 
     "profile.listings.$.id" : this._id 
    } 
} 

) });

现在我不能插入到数组中,因为$操作符。

回答

1

假设上市只是与id领域对象的数组,你可以这样做:

listings: { 
    type: [Object], 
    optional: true 
}, 
"listings.$.id": { 
    type: String, 
    optional: true 
} 

Listings.after.insert(function(userId, doc) { 
    var id = this._id; 
    Meteor.users.update({_id: userId }, { 
    $push : { 
     "profile.listings" : { id: id } 
    } 
    }); 
}); 

这改变了您的物品从一个字符串数组对象数组 - 你不能有一个属性的字符串上的id。然后这可以让你在profile.listings数组上执行$ push操作。如果你真的只是存储在列表的ID,虽然,你可以进一步简化这个:

listings: { 
    type: [String], 
    optional: true 
} 

Listings.after.insert(function(userId, doc) { 
    var id = this._id; 
    Meteor.users.update({_id: userId }, { 
    $push : { 
     "profile.listings" : id 
    } 
    }); 
}); 

也许你留下了一些代码,但是与您现有的架构,你不需要任何东西,但数组字符串 - 不需要id属性。

+0

在短短的一秒钟内就能拍出这张照片,非常感谢。 – bolle

+0

你是男人!非常感谢,我也复制了我如何使用个人资料图片模式。 – bolle