【发布时间】:2015-06-13 05:08:09
【问题描述】:
我得到了一遍又一遍的代码。它将一个新文档插入到名为 Services 的 Meteor Mongo 集合中,这是一个已在另一个文件 (Services = new Mongo.Collection("services")) 中实例化的全局对象。
Services.insert({
sku: 'hdrPhotos',
price: 100
});
Services.insert({
sku: 'twilightPhotos',
price: 100
});
Services.insert({
sku: 'videoClips',
price: 175
});
我想编写一个函数,该函数接受集合名称和要插入的对象数组,但我不确定如何在我的函数中将集合名称作为变量引用:
var insertIntoCollection = function(collectionName, arrayOfObjects){
for (index in arrayOfObjects){
// doesn't work
// collectionName.insert(arrayOfObjects[index]);
};
};
它会被称为
var serviceItems = [{
sku: 'hdrPhotos',
price: 100
},{
sku: 'twilightPhotos',
price: 100
},{
sku: 'videoClips',
price: 175
}];
insertIntoCollection("Services", serviceItems);
【问题讨论】:
标签: javascript meteor