Skip to Content
Node与Express开发(第2版)
book

Node与Express开发(第2版)

by Ethan Brown
June 2021
Intermediate to advanced
290 pages
9h 21m
Chinese
Posts & Telecom Press
Content preview from Node与Express开发(第2版)
134
|
第
13
章
price: Number,
tags: [String],
inSeason: Boolean,
available: Boolean,
requiresWaiver: Boolean,
maximumGuests: Number,
notes: String,
packagesSold: Number,
})
const Vacation = mongoose.model('Vacation', vacationSchema)
module.exports = Vacation
以上代码声明了度假产品模型的属性以及它们的类型。可以看到其中有几个字符串属性、
一些数值属性、两个布尔属性以及一个字符串数组(用
[String]
表示)。我们也可以给模式
定义方法。每个商品都有一个库存单位(
SKU
)
,虽然我们认为度假产品没有什么“库存”
可言,但从记账的角度来看,
SKU
是一个相当标准的概念,即便卖的不是有形的商品。
有了模式之后,使用
mongoose.model
来创建一个模型。在这一点上,
Vacation
跟传统的面
向对象编程中的类很相似。请注意,如果要定义方法,就必须在创建模型之前定义。
因为浮点数的本质,用
JavaScript
做财务计算时总是要十分小心。我们可以
把价格保存为美分而不是美元,这么做当然会有所帮助,但并不能完全解决
问题。对我们这个要求不高的旅游网站来说,倒是不用担心,但如果你的应
用涉及非常大或非常小的金额(例如,由利息或交易量带来的含小数的美分 ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.

Read now

Unlock full access

More than 5,000 organizations count on O’Reilly

AirBnbBlueOriginElectronic ArtsHomeDepotNasdaqRakutenTata Consultancy Services

QuotationMarkO’Reilly covers everything we've got, with content to help us build a world-class technology community, upgrade the capabilities and competencies of our teams, and improve overall team performance as well as their engagement.
Julian F.
Head of Cybersecurity
QuotationMarkI wanted to learn C and C++, but it didn't click for me until I picked up an O'Reilly book. When I went on the O’Reilly platform, I was astonished to find all the books there, plus live events and sandboxes so you could play around with the technology.
Addison B.
Field Engineer
QuotationMarkI’ve been on the O’Reilly platform for more than eight years. I use a couple of learning platforms, but I'm on O'Reilly more than anybody else. When you're there, you start learning. I'm never disappointed.
Amir M.
Data Platform Tech Lead
QuotationMarkI'm always learning. So when I got on to O'Reilly, I was like a kid in a candy store. There are playlists. There are answers. There's on-demand training. It's worth its weight in gold, in terms of what it allows me to do.
Mark W.
Embedded Software Engineer

You might also like

面向对象的思考过程(原书第5版)

面向对象的思考过程(原书第5版)

马特 魏斯费尔德
精通模块化JavaScript

精通模块化JavaScript

Nicolás Bevacqua

Publisher Resources

ISBN: 9787115565099