Node.JS中的架构重要吗?
我正在为一个我刚刚构build的表单创build一个巨大的模式…据说我的模式顺序必须模仿forms顺序,还是只能按照我把它们放入的顺序input所有的input? 下面的例子。 可以这样吗?
// link to mongoose var mongoose = require('mongoose'); // define the article schema var mapSchema = new mongoose.Schema({ created: { type: Date, default: Date.now }, dd1: { type: String, default: '' }, dd2: { type: String, default: '' }, com1: { type: String, default: '' }, com2: { type: String, default: '' } }); // make it public module.exports = mongoose.model('Map', mapSchema);
还是它必须是这样的?
// link to mongoose var mongoose = require('mongoose'); // define the article schema var mapSchema = new mongoose.Schema({ created: { type: Date, default: Date.now }, dd1: { type: String, default: '' }, com1: { type: String, default: '' }, dd2: { type: String, default: '' }, com2: { type: String, default: '' } }); // make it public module.exports = mongoose.model('Map', mapSchema);
我的模式订单是不是要模仿订单的forms呢,还是可以按照我把它们放入的顺序input所有的input?
mongoose.Schema
接受一个JavaScript对象作为参数。 所以你的问题归结为:
JavaScript对象是否知道它们的键被定义的顺序?
答案是:不,在JavaScript对象中不维护按键顺序。 JS规范明确指出对象是无序的键/值集合。 ( 比较 )
因此,这就是mongoose.Schema
即使绑定了也不能依赖于按键顺序,这意味着你可以自由地以任何你喜欢的方式订购这些按键。
我们也可以从另一端来处理这个问题:
像表单域顺序这样的前端更改是否可能会迫使我重写我的数据库后端代码?
答案是:不,这是不可能的。 我们甚至可以不考虑任何types的规范就可以驳回这个思想,因为它没有任何意义。