- 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量化投資指南:基礎(chǔ)、數(shù)據(jù)與實(shí)戰(zhàn)
- Beginning Java Data Structures and Algorithms
- OpenCV實(shí)例精解
- Windows系統(tǒng)管理與服務(wù)配置
- 區(qū)塊鏈架構(gòu)與實(shí)現(xiàn):Cosmos詳解
- Vue.js 3.x從入門(mén)到精通(視頻教學(xué)版)
- 技術(shù)領(lǐng)導(dǎo)力:程序員如何才能帶團(tuán)隊(duì)
- 單片機(jī)應(yīng)用技術(shù)
- Unity 5 for Android Essentials
- Java編程的邏輯
- Test-Driven JavaScript Development
- Maker基地嘉年華:玩轉(zhuǎn)樂(lè)動(dòng)魔盒學(xué)Scratch
- Java并發(fā)編程之美
- Mastering Elixir
- PhoneGap 4 Mobile Application Development Cookbook