- Build Applications with Meteor
- Dobrin Ganev
- 273字
- 2021-07-09 19:48:56
On the server
On the server-side, we have two files: index.js and insertData.js. There is also a private folder where we can define sample data that we can work with. Meteor will treat the private folder as an assets folder and will not bundle it with the rest of the execution code, which makes it a perfect place to have the test data.
In the private folder in file, products.json will have the following fields:

The fields of the Products collection are as follows:
id: This is the product ID.
title: This refers to a product title.
price: This is the price per unit.
inventory: This is the currently available product inventory in the store.
department: This refers to the department ID the product belongs to.
In the root of the application tree, we also have a shared folder where we can have modules used by both the client and the server.
In there, we define the two Collections that we will be using: Products and Cart:
export const ProductsCollection = new Mongo.Collection('products');
export const CartCollection = new Mongo.Collection('cart');
The simplest way to insert the data is through a looping in the JSON file and call the collection.insert() query on each iteration:
export default function() {
if (ProductsCollection.find().count() > 0) {
return;
}
const products = JSON.parse(Assets.getText('products.json'));
_.each(products, function(product) {
ProductsCollection.insert(product);
});
}
When we boot the server, this script will run and insert the data from the products.json. We insert data only if the Products collection is empty.
In the server/index.js, the code is as follows:
import insertData from './insertData';
Meteor.startup(() => {
insertData()
...
- Python數(shù)據(jù)分析入門與實戰(zhàn)
- LabVIEW2018中文版 虛擬儀器程序設計自學手冊
- 自己動手實現(xiàn)Lua:虛擬機、編譯器和標準庫
- 軟件測試項目實戰(zhàn)之性能測試篇
- 深入淺出Android Jetpack
- JavaScript+Vue+React全程實例
- JavaScript 程序設計案例教程
- OpenCV 4計算機視覺項目實戰(zhàn)(原書第2版)
- PyQt編程快速上手
- Anaconda數(shù)據(jù)科學實戰(zhàn)
- Xamarin Cross-Platform Development Cookbook
- Java EE 程序設計
- Visual C++程序開發(fā)范例寶典
- Visual Basic 開發(fā)從入門到精通
- 軟件自動化測試實戰(zhàn)解析:基于Python3編程語言