From 35cdfb068ac68b4de0a250c445b9d50e02987f37 Mon Sep 17 00:00:00 2001 From: jiangyuzhen <1191973018@qq.com> Date: Mon, 23 Jul 2018 23:51:03 +0800 Subject: [PATCH 1/4] [Jane] Translaion 1-3 sections --- .../mongodb-and-mongoose.json | 16 +++++++++++++++- 1 file changed, 15 insertions(+), 1 deletion(-) diff --git a/05-apis-and-microservices/mongodb-and-mongoose.json b/05-apis-and-microservices/mongodb-and-mongoose.json index 3a661a8..2e9166a 100644 --- a/05-apis-and-microservices/mongodb-and-mongoose.json +++ b/05-apis-and-microservices/mongodb-and-mongoose.json @@ -8,19 +8,23 @@ "id": "587d7fb6367417b2b2512c06", "title": "Install and Set Up Mongoose", "description": [ - "Add mongodb and mongoose to the project’s package.json. Then require mongoose. Store your mLab database URI in the private .env file as MONGO_URI. Connect to the database using mongoose.connect()" + "Add mongodb and mongoose to the project’s package.json. Then require mongoose. Store your mLab database URI in the private .env file as MONGO_URI. Connect to the database using mongoose.connect()", + "在 package.json 文件中添加 mongodb 和 mongoose 依赖. 然后 require('mongoose'). 将 mLab 数据库 URI 作为 MONGO_URI 变量存储在私有 .env 文件中. 使用 mongoose.connect() 命令连接数据库" ], "tests": [ { "text": "\"mongodb\" dependency should be in package.json", + "text": "在 package.json 文件中应该有 \"mongodb\" 依赖", "testString": "getUserInput => $.get(getUserInput('url') + '/_api/file/package.json').then(data => { var packJson = JSON.parse(data); assert.property(packJson.dependencies, 'mongodb'); }, xhr => { throw new Error(xhr.responseText); })" }, { "text": "\"mongoose\" dependency should be in package.json", + "text": "在 package.json 文件中应该有 \"mongoose\" 依赖", "testString": "getUserInput => $.get(getUserInput('url') + '/_api/file/package.json').then(data => { var packJson = JSON.parse(data); assert.property(packJson.dependencies, 'mongoose'); }, xhr => { throw new Error(xhr.responseText); })" }, { "text": "\"mongoose\" should be connected to a database", + "text": "\"mongoose\" 应该已经连接数据库", "testString": "getUserInput => $.get(getUserInput('url') + '/_api/is-mongoose-ok').then(data => {assert.isTrue(data.isMongooseOk, 'mongoose is not connected')}, xhr => { throw new Error(xhr.responseText); })" } ], @@ -33,9 +37,13 @@ "title": "Create a Model", "description": [ "First of all we need a Schema. Each schema maps to a MongoDB collection. It defines the shape of the documents within that collection.", + "首先,我们需要一个 Schema. 每一个 schema 对应一个 mongoDB collection 并且在那个 collection 里面定义了 documents 的模型", "Schemas are building block for Models. They can be nested to create complex models, but in this case we’ll keep things simple.", + "Schemas 是 Models 的构建块. 它们可以嵌套来创建复杂的模型,但是这里,我们会保持简单.", "A model allows you to create instances of your objects, called documents.", + "Model 可以被实例化,实例化后的对象称为 document", "Create a person having this prototype :", + "创建一个拥有以下 prototype 的 person 对象:", "- Person Prototype -", "--------------------", "name : string [required]", @@ -44,9 +52,12 @@ "Use the mongoose basic schema types. If you want you can also add", "more fields, use simple validators like required or unique,", "and set default values. See the mongoose docs.", + "你可以使用 schema 的基础 types 去添加更多的字段。比如使用 required 或者 unique 这样的简单验证去设置默认值. 看 mongoose 文档.", "[C]RUD Part I - CREATE", "Note: Glitch is a real server, and in real servers the interactions with the db happen in handler functions. These function are executed when some event happens (e.g. someone hits an endpoint on your API). We’ll follow the same approach in these exercises. The done() function is a callback that tells us that we can proceed after completing an asynchronous operation such as inserting, searching, updating or deleting. It’s following the Node convention and should be called as done(null, data) on success, or done(err) on error.", + "注意: Glitch 是一个真实的服务, 并且通过 handler 函数和 db 进行交互. 这些函数通过一些事件去触发(e.g. 有人从终端调用了你的 API). 我们在这些联系中遵循同样的方法. 我们在完成如 nserting, searching, updating 或者 deleting 这样的异步操作后接着回调 done() 函数. 它遵循 Node 的惯例,需要在 success 时回调 done(null, data), 在 error 时回调 done(err)。", "Warning - When interacting with remote services, errors may occur !", + "Warning - 当与远程服务器交互时可能发生错误!", "/* Example */", "var someFunc = function(done) {", " //... do something (risky) ...", @@ -57,6 +68,7 @@ "tests": [ { "text": "Creating an instance from a mongoose schema should succeed", + "text": "成功创建一个 schema 实例", "testString": "getUserInput => $.post(getUserInput('url') + '/_api/mongoose-model', {name: 'Mike', age: 28, favoriteFoods: ['pizza', 'cheese']}).then(data => { assert.equal(data.name, 'Mike', '\"model.name\" is not what expected'); assert.equal(data.age, '28', '\"model.age\" is not what expected'); assert.isArray(data.favoriteFoods, '\"model.favoriteFoods\" is not an Array'); assert.include(data.favoriteFoods, 'pizza', '\"model.favoriteFoods\" does not include the expected items'); assert.include(data.favoriteFoods, 'cheese', '\"model.favoriteFoods\" does not include the expected items'); }, xhr => { throw new Error(xhr.responseText); })" } ], @@ -69,6 +81,7 @@ "title": "Create and Save a Record of a Model", "description": [ "Create a document instance using the Person constructor you build before. Pass to the constructor an object having the fields name, age, and favoriteFoods. Their types must be conformant to the ones in the Person Schema. Then call the method document.save() on the returned document instance. Pass to it a callback using the Node convention. This is a common pattern, all the following CRUD methods take a callback function like this as the last argument.", + "使用 Person 的 constructor 函数可以创建一个 document 对象, 该对象包含 name, age 和 favoriteFoods 字段. 这些字段的类型必须符合 Person Schema 里面定义的类型. 然后调用 document.save(). 使用 Node 惯例传递 callback. 通常情况下,所有的 CRUD(增查改删) 方法都会像下面一样作为最后一个参数去执行一个 callback() ", "/* Example */", "// ...", "person.save(function(err, data) {", @@ -78,6 +91,7 @@ "tests": [ { "text": "Creating and saving a db item should succeed", + "text": "成功创建一条 db 并保存", "testString": "getUserInput => $.get(getUserInput('url') + '/_api/create-and-save-person').then(data => { assert.isString(data.name, '\"item.name\" should be a String'); assert.isNumber(data.age, '28', '\"item.age\" should be a Number'); assert.isArray(data.favoriteFoods, '\"item.favoriteFoods\" should be an Array'); assert.equal(data.__v, 0, 'The db item should be not previously edited'); }, xhr => { throw new Error(xhr.responseText); })" } ], From cd4b9dc53a87961b5c9dab4d1b8f4a10049c0a1a Mon Sep 17 00:00:00 2001 From: jiangyuzhen <1191973018@qq.com> Date: Fri, 27 Jul 2018 00:32:38 +0800 Subject: [PATCH 2/4] [yuzhen] Translate mongodb-and-mongoose 4-7 Section && Processing feedback --- .../mongodb-and-mongoose.json | 48 +++++++------------ 1 file changed, 18 insertions(+), 30 deletions(-) diff --git a/05-apis-and-microservices/mongodb-and-mongoose.json b/05-apis-and-microservices/mongodb-and-mongoose.json index 2e9166a..a50e776 100644 --- a/05-apis-and-microservices/mongodb-and-mongoose.json +++ b/05-apis-and-microservices/mongodb-and-mongoose.json @@ -8,22 +8,18 @@ "id": "587d7fb6367417b2b2512c06", "title": "Install and Set Up Mongoose", "description": [ - "Add mongodb and mongoose to the project’s package.json. Then require mongoose. Store your mLab database URI in the private .env file as MONGO_URI. Connect to the database using mongoose.connect()", - "在 package.json 文件中添加 mongodb 和 mongoose 依赖. 然后 require('mongoose'). 将 mLab 数据库 URI 作为 MONGO_URI 变量存储在私有 .env 文件中. 使用 mongoose.connect() 命令连接数据库" + "在 package.json 文件中添加 MongoDB 和 Mongoose 依赖, 将 mLab 数据库的 URI 作为 MONGO_URI 变量存储在私有 .env 文件中。然后 require('mongoose'),使用 mongoose.connect() 命令来连接数据库。" ], "tests": [ { - "text": "\"mongodb\" dependency should be in package.json", "text": "在 package.json 文件中应该有 \"mongodb\" 依赖", "testString": "getUserInput => $.get(getUserInput('url') + '/_api/file/package.json').then(data => { var packJson = JSON.parse(data); assert.property(packJson.dependencies, 'mongodb'); }, xhr => { throw new Error(xhr.responseText); })" }, { - "text": "\"mongoose\" dependency should be in package.json", "text": "在 package.json 文件中应该有 \"mongoose\" 依赖", "testString": "getUserInput => $.get(getUserInput('url') + '/_api/file/package.json').then(data => { var packJson = JSON.parse(data); assert.property(packJson.dependencies, 'mongoose'); }, xhr => { throw new Error(xhr.responseText); })" }, { - "text": "\"mongoose\" should be connected to a database", "text": "\"mongoose\" 应该已经连接数据库", "testString": "getUserInput => $.get(getUserInput('url') + '/_api/is-mongoose-ok').then(data => {assert.isTrue(data.isMongooseOk, 'mongoose is not connected')}, xhr => { throw new Error(xhr.responseText); })" } @@ -37,13 +33,10 @@ "title": "Create a Model", "description": [ "First of all we need a Schema. Each schema maps to a MongoDB collection. It defines the shape of the documents within that collection.", - "首先,我们需要一个 Schema. 每一个 schema 对应一个 mongoDB collection 并且在那个 collection 里面定义了 documents 的模型", - "Schemas are building block for Models. They can be nested to create complex models, but in this case we’ll keep things simple.", - "Schemas 是 Models 的构建块. 它们可以嵌套来创建复杂的模型,但是这里,我们会保持简单.", - "A model allows you to create instances of your objects, called documents.", - "Model 可以被实例化,实例化后的对象称为 document", - "Create a person having this prototype :", - "创建一个拥有以下 prototype 的 person 对象:", + "首先,我们需要一个 Schema,每一个 Schema 对应一个 MongoDB collection, 并且在那个 collection 里面定义 documents 的模型。", + "Schemas 是 Models 的构建块。它们可以嵌套来创建复杂的模型,但是这里,我们只学习简单的用法。", + "Model 可以被实例化,实例化后的对象称为 documents", + "创建一个拥有以下 Prototype 的 Person 对象:", "- Person Prototype -", "--------------------", "name : string [required]", @@ -52,12 +45,10 @@ "Use the mongoose basic schema types. If you want you can also add", "more fields, use simple validators like required or unique,", "and set default values. See the mongoose docs.", - "你可以使用 schema 的基础 types 去添加更多的字段。比如使用 required 或者 unique 这样的简单验证去设置默认值. 看 mongoose 文档.", + "你可以使用 基础的 SchemaTypes 去添加更多的字段,比如使用 required 或者 unique 这样的简单验证去设置默认值. 参考 Mongoose 文档.", "[C]RUD Part I - CREATE", - "Note: Glitch is a real server, and in real servers the interactions with the db happen in handler functions. These function are executed when some event happens (e.g. someone hits an endpoint on your API). We’ll follow the same approach in these exercises. The done() function is a callback that tells us that we can proceed after completing an asynchronous operation such as inserting, searching, updating or deleting. It’s following the Node convention and should be called as done(null, data) on success, or done(err) on error.", - "注意: Glitch 是一个真实的服务, 并且通过 handler 函数和 db 进行交互. 这些函数通过一些事件去触发(e.g. 有人从终端调用了你的 API). 我们在这些联系中遵循同样的方法. 我们在完成如 nserting, searching, updating 或者 deleting 这样的异步操作后接着回调 done() 函数. 它遵循 Node 的惯例,需要在 success 时回调 done(null, data), 在 error 时回调 done(err)。", - "Warning - When interacting with remote services, errors may occur !", - "Warning - 当与远程服务器交互时可能发生错误!", + "注意: Glitch 是一个真实的服务, 并且通过 handler 函数和 db 进行交互。 这些函数通过一些事件去触发(例如:有人从终端调用了你的 API), 我们在这些练习中遵循同样的方法。 比如,我们在完成 nserting、 searching、 updating 或者 deleting 这样的异步操作后接着回调 done() 函数。 它遵循 Node 的惯例,需要在 success 时回调 done(null, data), 在 error 时回调 done(err)。", + "Warning - 当与远程服务器交互时可能发生错误!", "/* Example */", "var someFunc = function(done) {", " //... do something (risky) ...", @@ -67,8 +58,7 @@ ], "tests": [ { - "text": "Creating an instance from a mongoose schema should succeed", - "text": "成功创建一个 schema 实例", + "text": "成功创建一个 Schema 实例", "testString": "getUserInput => $.post(getUserInput('url') + '/_api/mongoose-model', {name: 'Mike', age: 28, favoriteFoods: ['pizza', 'cheese']}).then(data => { assert.equal(data.name, 'Mike', '\"model.name\" is not what expected'); assert.equal(data.age, '28', '\"model.age\" is not what expected'); assert.isArray(data.favoriteFoods, '\"model.favoriteFoods\" is not an Array'); assert.include(data.favoriteFoods, 'pizza', '\"model.favoriteFoods\" does not include the expected items'); assert.include(data.favoriteFoods, 'cheese', '\"model.favoriteFoods\" does not include the expected items'); }, xhr => { throw new Error(xhr.responseText); })" } ], @@ -80,8 +70,7 @@ "id": "587d7fb6367417b2b2512c09", "title": "Create and Save a Record of a Model", "description": [ - "Create a document instance using the Person constructor you build before. Pass to the constructor an object having the fields name, age, and favoriteFoods. Their types must be conformant to the ones in the Person Schema. Then call the method document.save() on the returned document instance. Pass to it a callback using the Node convention. This is a common pattern, all the following CRUD methods take a callback function like this as the last argument.", - "使用 Person 的 constructor 函数可以创建一个 document 对象, 该对象包含 name, age 和 favoriteFoods 字段. 这些字段的类型必须符合 Person Schema 里面定义的类型. 然后调用 document.save(). 使用 Node 惯例传递 callback. 通常情况下,所有的 CRUD(增查改删) 方法都会像下面一样作为最后一个参数去执行一个 callback() ", + "使用 Person 的 constructor(构造器) 函数可以创建一个 document 对象,该对象包含 nameagefavoriteFoods 字段。 这些字段的类型必须符合 Person Schema 里面定义的类型。 然后调用 document.save()。 使用 Node 惯例传递 callback。通常情况下,所有的 CRUD(增查改删) 方法都会像下面一样作为最后一个参数去执行一个 callback()。", "/* Example */", "// ...", "person.save(function(err, data) {", @@ -90,7 +79,6 @@ ], "tests": [ { - "text": "Creating and saving a db item should succeed", "text": "成功创建一条 db 并保存", "testString": "getUserInput => $.get(getUserInput('url') + '/_api/create-and-save-person').then(data => { assert.isString(data.name, '\"item.name\" should be a String'); assert.isNumber(data.age, '28', '\"item.age\" should be a Number'); assert.isArray(data.favoriteFoods, '\"item.favoriteFoods\" should be an Array'); assert.equal(data.__v, 0, 'The db item should be not previously edited'); }, xhr => { throw new Error(xhr.responseText); })" } @@ -103,7 +91,7 @@ "id": "587d7fb7367417b2b2512c0a", "title": "Create Many Records with model.create()", "description": [ - "Sometimes you need to create many instances of your models, e.g. when seeding a database with initial data. Model.create() takes an array of objects like [{name: 'John', ...}, {...}, ...] as the first argument, and saves them all in the db. Create many people with Model.create(), using the function argument arrayOfPeople." + "有时你需要创建很多的 model 实例。 例如:在使用初始数据为数据库初始化时, Model.create() 接受一组像 [{name: 'John', ...}, {...}, ...] 的数组作为第一个参数,并将其保存到数据库。使用 arrayOfPeople 作为 Model.create() 的参数创建很多个 people 实例。" ], "tests": [ { @@ -119,12 +107,12 @@ "id": "587d7fb7367417b2b2512c0b", "title": "Use model.find() to Search Your Database", "description": [ - "Find all the people having a given name, using Model.find() -> [Person]", - "In its simplest usage, Model.find() accepts a query document (a JSON object ) as the first argument, then a callback. It returns an array of matches. It supports an extremely wide range of search options. Check it in the docs. Use the function argument personName as search key." + "使用 Model.find() -> [Person] 来查询给定名称的所有的人。", + "最简单的用法: Model.find() 接受一个查询的 document (一个 JSON 对象 ) 作为第一参数, 然后是回调。 它将返回匹配到的项目组成的数组。 这个支持极其广泛的搜索选项。 使用人名作为搜索的关键词, 来校验它。" ], "tests": [ { - "text": "Find all items corresponding to a criteria should succeed", + "text": "成功找到所有符合条件的 item(项目)。", "testString": "getUserInput => $.post(getUserInput('url') + '/_api/find-all-by-name', {name: 'r@nd0mN4m3', age: 24, favoriteFoods: ['pizza']}).then(data => { assert.isArray(data, 'the response should be an Array'); assert.equal(data[0].name, 'r@nd0mN4m3', 'item.name is not what expected'); assert.equal(data[0].__v, 0, 'The item should be not previously edited'); }, xhr => { throw new Error(xhr.responseText); })" } ], @@ -136,11 +124,11 @@ "id": "587d7fb7367417b2b2512c0c", "title": "Use model.findOne() to Return a Single Matching Document from Your Database", "description": [ - "Model.findOne() behaves like .find(), but it returns only one document (not an array), even if there are items. It is especially useful when searching by properties that you have declared as unique. Find just one person which has a certain food in her favorites, using Model.findOne() -> Person. Use the function argument food as search key." + "Model.findOne() 表现像 Model.find(), 但是它仅仅返回一个 document (而不是一个数组), 即使数据库里有很多条 item(项目)。 当你按声明成 unique 的属性进行搜索时,Model.findOne() 尤其有用。 把食物作为 Model.findOne() -> Person 的参数,来找到一个在她的爱好中有某一食物的人。" ], "tests": [ { - "text": "Find one item should succeed", + "text": "成功找到一个 item(项目)。", "testString": "getUserInput => $.post(getUserInput('url') + '/_api/find-one-by-food', {name: 'Gary', age: 46, favoriteFoods: ['chicken salad']}).then(data => { assert.equal(data.name, 'Gary', 'item.name is not what expected'); assert.deepEqual(data.favoriteFoods, ['chicken salad'], 'item.favoriteFoods is not what expected'); assert.equal(data.__v, 0, 'The item should be not previously edited'); }, xhr => { throw new Error(xhr.responseText); })" } ], @@ -152,11 +140,11 @@ "id": "587d7fb7367417b2b2512c0d", "title": "Use model.findById() to Search Your Database By _id", "description": [ - "When saving a document, mongodb automatically adds the field _id, and set it to a unique alphanumeric key. Searching by _id is an extremely frequent operation, so moongose provides a dedicated method for it. Find the (only!!) person having a given _id, using Model.findById() -> Person. Use the function argument personId as search key." + "当我们保存一个 document, MongoDB 自动添加 _id 字段, 并给该字段设置 unique(唯一) 属性。通过 _id 搜索是一个非常频繁的操作,所以 Mongose 为它提供了一个专门的方法。使用人物 Id 作为参数,执行 Model.findById() -> Person, 找到这个_id 对应的唯一的一个人。" ], "tests": [ { - "text": "Find an item by Id should succeed", + "text": "通过 Id 成功找到对应的 item(项目)。", "testString": "getUserInput => $.get(getUserInput('url') + '/_api/find-by-id').then(data => { assert.equal(data.name, 'test', 'item.name is not what expected'); assert.equal(data.age, 0, 'item.age is not what expected'); assert.deepEqual(data.favoriteFoods, ['none'], 'item.favoriteFoods is not what expected'); assert.equal(data.__v, 0, 'The item should be not previously edited'); }, xhr => { throw new Error(xhr.responseText); })" } ], From aeb8bce84eaaf112a46ba7d83635b56543d7115b Mon Sep 17 00:00:00 2001 From: jiangyuzhen <1191973018@qq.com> Date: Sun, 29 Jul 2018 00:00:45 +0800 Subject: [PATCH 3/4] [yuzhen]update --- .../mongodb-and-mongoose.json | 63 +++++++++---------- 1 file changed, 30 insertions(+), 33 deletions(-) diff --git a/05-apis-and-microservices/mongodb-and-mongoose.json b/05-apis-and-microservices/mongodb-and-mongoose.json index a50e776..71c649a 100644 --- a/05-apis-and-microservices/mongodb-and-mongoose.json +++ b/05-apis-and-microservices/mongodb-and-mongoose.json @@ -8,20 +8,20 @@ "id": "587d7fb6367417b2b2512c06", "title": "Install and Set Up Mongoose", "description": [ - "在 package.json 文件中添加 MongoDB 和 Mongoose 依赖, 将 mLab 数据库的 URI 作为 MONGO_URI 变量存储在私有 .env 文件中。然后 require('mongoose'),使用 mongoose.connect() 命令来连接数据库。" + "在 package.json 文件中添加 MongoDB 和 Mongoose 依赖,将 mLab 数据库的 URI 作为 MONGO_URI 变量存储在私有 .env 文件中。然后 require('mongoose'),使用 mongoose.connect() 命令来连接数据库。" ], "tests": [ { - "text": "在 package.json 文件中应该有 \"mongodb\" 依赖", - "testString": "getUserInput => $.get(getUserInput('url') + '/_api/file/package.json').then(data => { var packJson = JSON.parse(data); assert.property(packJson.dependencies, 'mongodb'); }, xhr => { throw new Error(xhr.responseText); })" + "text": "在 package.json 文件中应该有 \"mongodb\"依赖。", + "testString": "getUserInput => $.get(getUserInput('url') +'/_api/file/package.json').then(data => { var packJson = JSON.parse(data); assert.property(packJson.dependencies,'mongodb'); }, xhr => { throw new Error(xhr.responseText); })" }, { - "text": "在 package.json 文件中应该有 \"mongoose\" 依赖", - "testString": "getUserInput => $.get(getUserInput('url') + '/_api/file/package.json').then(data => { var packJson = JSON.parse(data); assert.property(packJson.dependencies, 'mongoose'); }, xhr => { throw new Error(xhr.responseText); })" + "text": "在 package.json 文件中应该有 \"mongoose\"依赖。", + "testString": "getUserInput => $.get(getUserInput('url') +'/_api/file/package.json').then(data => { var packJson = JSON.parse(data); assert.property(packJson.dependencies,'mongoose'); }, xhr => { throw new Error(xhr.responseText); })" }, { - "text": "\"mongoose\" 应该已经连接数据库", - "testString": "getUserInput => $.get(getUserInput('url') + '/_api/is-mongoose-ok').then(data => {assert.isTrue(data.isMongooseOk, 'mongoose is not connected')}, xhr => { throw new Error(xhr.responseText); })" + "text": "\"mongoose\"应该已经连接数据库。", + "testString": "getUserInput => $.get(getUserInput('url') +'/_api/is-mongoose-ok').then(data => {assert.isTrue(data.isMongooseOk,'mongoose is not connected')}, xhr => { throw new Error(xhr.responseText); })" } ], "solutions": [], @@ -32,22 +32,18 @@ "id": "587d7fb6367417b2b2512c07", "title": "Create a Model", "description": [ - "First of all we need a Schema. Each schema maps to a MongoDB collection. It defines the shape of the documents within that collection.", - "首先,我们需要一个 Schema,每一个 Schema 对应一个 MongoDB collection, 并且在那个 collection 里面定义 documents 的模型。", + "首先,我们需要一个 Schema,每一个 Schema 对应一个 MongoDB collection,并且在那个 collection 里面定义 documents 的模型。", "Schemas 是 Models 的构建块。它们可以嵌套来创建复杂的模型,但是这里,我们只学习简单的用法。", - "Model 可以被实例化,实例化后的对象称为 documents", + "Model 可以被实例化,实例化后的对象称为 documents。", "创建一个拥有以下 Prototype 的 Person 对象:", "- Person Prototype -", "--------------------", "name : string [required]", "age : number", "favoriteFoods : array of strings (*) ", - "Use the mongoose basic schema types. If you want you can also add", - "more fields, use simple validators like required or unique,", - "and set default values. See the mongoose docs.", - "你可以使用 基础的 SchemaTypes 去添加更多的字段,比如使用 required 或者 unique 这样的简单验证去设置默认值. 参考 Mongoose 文档.", + "你可以使用基础的 SchemaTypes 去添加更多的字段,比如使用 required 或者 unique 这样的简单验证去设置默认值。 参考 Mongoose 文档 。", "[C]RUD Part I - CREATE", - "注意: Glitch 是一个真实的服务, 并且通过 handler 函数和 db 进行交互。 这些函数通过一些事件去触发(例如:有人从终端调用了你的 API), 我们在这些练习中遵循同样的方法。 比如,我们在完成 nserting、 searching、 updating 或者 deleting 这样的异步操作后接着回调 done() 函数。 它遵循 Node 的惯例,需要在 success 时回调 done(null, data), 在 error 时回调 done(err)。", + "注意: Glitch 是一个真实的服务,并且通过 handler 函数和 db 进行交互。 这些函数通过一些事件去触发 (例如:有人从终端调用了你的 API),我们在这些练习中遵循同样的方法。 比如,我们在完成 nserting、 searching、 updating 或者 deleting 这样的异步操作后接着回调 done() 函数。 它遵循 Node 的惯例,需要在 success 时回调 done(null, data), 在 error 时回调 done(err)。", "Warning - 当与远程服务器交互时可能发生错误!", "/* Example */", "var someFunc = function(done) {", @@ -58,8 +54,8 @@ ], "tests": [ { - "text": "成功创建一个 Schema 实例", - "testString": "getUserInput => $.post(getUserInput('url') + '/_api/mongoose-model', {name: 'Mike', age: 28, favoriteFoods: ['pizza', 'cheese']}).then(data => { assert.equal(data.name, 'Mike', '\"model.name\" is not what expected'); assert.equal(data.age, '28', '\"model.age\" is not what expected'); assert.isArray(data.favoriteFoods, '\"model.favoriteFoods\" is not an Array'); assert.include(data.favoriteFoods, 'pizza', '\"model.favoriteFoods\" does not include the expected items'); assert.include(data.favoriteFoods, 'cheese', '\"model.favoriteFoods\" does not include the expected items'); }, xhr => { throw new Error(xhr.responseText); })" + "text": "成功创建一个 Schema 实例。", + "testString": "getUserInput => $.post(getUserInput('url') +'/_api/mongoose-model', {name:'Mike', age: 28, favoriteFoods: ['pizza','cheese']}).then(data => { assert.equal(data.name,'Mike','\"model.name\" is not what expected'); assert.equal(data.age,'28','\"model.age\" is not what expected'); assert.isArray(data.favoriteFoods,'\"model.favoriteFoods\" is not an Array'); assert.include(data.favoriteFoods,'pizza','\"model.favoriteFoods\" does not include the expected items'); assert.include(data.favoriteFoods,'cheese','\"model.favoriteFoods\" does not include the expected items'); }, xhr => { throw new Error(xhr.responseText); })" } ], "solutions": [], @@ -79,8 +75,8 @@ ], "tests": [ { - "text": "成功创建一条 db 并保存", - "testString": "getUserInput => $.get(getUserInput('url') + '/_api/create-and-save-person').then(data => { assert.isString(data.name, '\"item.name\" should be a String'); assert.isNumber(data.age, '28', '\"item.age\" should be a Number'); assert.isArray(data.favoriteFoods, '\"item.favoriteFoods\" should be an Array'); assert.equal(data.__v, 0, 'The db item should be not previously edited'); }, xhr => { throw new Error(xhr.responseText); })" + "text": "成功创建一条 db 并保存。", + "testString": "getUserInput => $.get(getUserInput('url') +'/_api/create-and-save-person').then(data => { assert.isString(data.name,'\"item.name\" should be a String'); assert.isNumber(data.age,'28','\"item.age\" should be a Number'); assert.isArray(data.favoriteFoods,'\"item.favoriteFoods\" should be an Array'); assert.equal(data.__v, 0,'The db item should be not previously edited'); }, xhr => { throw new Error(xhr.responseText); })" } ], "solutions": [], @@ -91,12 +87,13 @@ "id": "587d7fb7367417b2b2512c0a", "title": "Create Many Records with model.create()", "description": [ - "有时你需要创建很多的 model 实例。 例如:在使用初始数据为数据库初始化时, Model.create() 接受一组像 [{name: 'John', ...}, {...}, ...] 的数组作为第一个参数,并将其保存到数据库。使用 arrayOfPeople 作为 Model.create() 的参数创建很多个 people 实例。" + "有时你需要创建很多的 model 实例。 例如:在使用初始数据为数据库初始化时, Model.create() 接受一组像 [{name:'John', ...}, {...}, ...] 的数组作为第一个参数,并将其保存到数据库。使用 arrayOfPeople 作为 Model.create() 的参数创建很多个 people 实例。" ], "tests": [ { "text": "Creating many db items at once should succeed", - "testString": "getUserInput => $.ajax({url: getUserInput('url') + '/_api/create-many-people', type: 'POST', contentType:'application/json', data: JSON.stringify([{name: 'John', age: 24, favoriteFoods: ['pizza', 'salad']}, {name: 'Mary', age: 21, favoriteFoods: ['onions', 'chicken']}])}).then(data => { assert.isArray(data, 'the response should be an array'); assert.equal(data.length, 2, 'the response does not contain the expected number of items'); assert.equal(data[0].name, 'John', 'The first item is not correct'); assert.equal(data[0].__v, 0, 'The first item should be not previously edited'); assert.equal(data[1].name, 'Mary', 'The second item is not correct'); assert.equal(data[1].__v, 0, 'The second item should be not previously edited'); }, xhr => { throw new Error(xhr.responseText); })" + "text": "一次性创建多个 item(项目)。", + "testString": "getUserInput => $.ajax({url: getUserInput('url') +'/_api/create-many-people', type:'POST', contentType:'application/json', data: JSON.stringify([{name:'John', age: 24, favoriteFoods: ['pizza','salad']}, {name:'Mary', age: 21, favoriteFoods: ['onions','chicken']}])}).then(data => { assert.isArray(data,'the response should be an array'); assert.equal(data.length, 2,'the response does not contain the expected number of items'); assert.equal(data[0].name,'John','The first item is not correct'); assert.equal(data[0].__v, 0,'The first item should be not previously edited'); assert.equal(data[1].name,'Mary','The second item is not correct'); assert.equal(data[1].__v, 0,'The second item should be not previously edited'); }, xhr => { throw new Error(xhr.responseText); })" } ], "solutions": [], @@ -113,7 +110,7 @@ "tests": [ { "text": "成功找到所有符合条件的 item(项目)。", - "testString": "getUserInput => $.post(getUserInput('url') + '/_api/find-all-by-name', {name: 'r@nd0mN4m3', age: 24, favoriteFoods: ['pizza']}).then(data => { assert.isArray(data, 'the response should be an Array'); assert.equal(data[0].name, 'r@nd0mN4m3', 'item.name is not what expected'); assert.equal(data[0].__v, 0, 'The item should be not previously edited'); }, xhr => { throw new Error(xhr.responseText); })" + "testString": "getUserInput => $.post(getUserInput('url') +'/_api/find-all-by-name', {name:'r@nd0mN4m3', age: 24, favoriteFoods: ['pizza']}).then(data => { assert.isArray(data,'the response should be an Array'); assert.equal(data[0].name,'r@nd0mN4m3','item.name is not what expected'); assert.equal(data[0].__v, 0,'The item should be not previously edited'); }, xhr => { throw new Error(xhr.responseText); })" } ], "solutions": [], @@ -124,12 +121,12 @@ "id": "587d7fb7367417b2b2512c0c", "title": "Use model.findOne() to Return a Single Matching Document from Your Database", "description": [ - "Model.findOne() 表现像 Model.find(), 但是它仅仅返回一个 document (而不是一个数组), 即使数据库里有很多条 item(项目)。 当你按声明成 unique 的属性进行搜索时,Model.findOne() 尤其有用。 把食物作为 Model.findOne() -> Person 的参数,来找到一个在她的爱好中有某一食物的人。" + "Model.findOne() 表现像 Model.find(), 但是它仅仅返回一个 document (而不是一个数组),即使数据库里有很多条 item(项目)。 当你按声明成 unique 的属性进行搜索时,Model.findOne() 尤其有用。 把食物作为 Model.findOne() -> Person 的参数,来找到一个在她的爱好中有某一食物的人。" ], "tests": [ { "text": "成功找到一个 item(项目)。", - "testString": "getUserInput => $.post(getUserInput('url') + '/_api/find-one-by-food', {name: 'Gary', age: 46, favoriteFoods: ['chicken salad']}).then(data => { assert.equal(data.name, 'Gary', 'item.name is not what expected'); assert.deepEqual(data.favoriteFoods, ['chicken salad'], 'item.favoriteFoods is not what expected'); assert.equal(data.__v, 0, 'The item should be not previously edited'); }, xhr => { throw new Error(xhr.responseText); })" + "testString": "getUserInput => $.post(getUserInput('url') +'/_api/find-one-by-food', {name:'Gary', age: 46, favoriteFoods: ['chicken salad']}).then(data => { assert.equal(data.name,'Gary','item.name is not what expected'); assert.deepEqual(data.favoriteFoods, ['chicken salad'],'item.favoriteFoods is not what expected'); assert.equal(data.__v, 0,'The item should be not previously edited'); }, xhr => { throw new Error(xhr.responseText); })" } ], "solutions": [], @@ -140,12 +137,12 @@ "id": "587d7fb7367417b2b2512c0d", "title": "Use model.findById() to Search Your Database By _id", "description": [ - "当我们保存一个 document, MongoDB 自动添加 _id 字段, 并给该字段设置 unique(唯一) 属性。通过 _id 搜索是一个非常频繁的操作,所以 Mongose 为它提供了一个专门的方法。使用人物 Id 作为参数,执行 Model.findById() -> Person, 找到这个_id 对应的唯一的一个人。" + "当我们保存一个 document, MongoDB 自动添加 _id 字段, 并给该字段设置 unique(唯一) 属性。通过 _id 搜索是一个非常频繁的操作,所以 Mongose 为它提供了一个专门的方法。使用人物 Id 作为参数,执行 Model.findById() -> Person, 找到这个_id 对应的唯一的一个人。" ], "tests": [ { "text": "通过 Id 成功找到对应的 item(项目)。", - "testString": "getUserInput => $.get(getUserInput('url') + '/_api/find-by-id').then(data => { assert.equal(data.name, 'test', 'item.name is not what expected'); assert.equal(data.age, 0, 'item.age is not what expected'); assert.deepEqual(data.favoriteFoods, ['none'], 'item.favoriteFoods is not what expected'); assert.equal(data.__v, 0, 'The item should be not previously edited'); }, xhr => { throw new Error(xhr.responseText); })" + "testString": "getUserInput => $.get(getUserInput('url') +'/_api/find-by-id').then(data => { assert.equal(data.name,'test','item.name is not what expected'); assert.equal(data.age, 0,'item.age is not what expected'); assert.deepEqual(data.favoriteFoods, ['none'],'item.favoriteFoods is not what expected'); assert.equal(data.__v, 0,'The item should be not previously edited'); }, xhr => { throw new Error(xhr.responseText); })" } ], "solutions": [], @@ -157,13 +154,13 @@ "title": "Perform Classic Updates by Running Find, Edit, then Save", "description": [ "In the good old days this was what you needed to do if you wanted to edit a document and be able to use it somehow e.g. sending it back in a server response. Mongoose has a dedicated updating method : Model.update(). It is binded to the low-level mongo driver. It can bulk edit many documents matching certain criteria, but it doesn’t send back the updated document, only a ‘status’ message. Furthermore it makes model validations difficult, because it just directly calls the mongo driver.", - "Find a person by _id ( use any of the above methods ) with the parameter personId as search key. Add “hamburger” to the list of her favoriteFoods (you can use Array.push()). Then - inside the find callback - save() the updated Person.", + "Find a person by _id (use any of the above methods) with the parameter personId as search key. Add “hamburger” to the list of her favoriteFoods (you can use Array.push()). Then - inside the find callback - save() the updated Person.", "[*] Hint: This may be tricky if in your Schema you declared favoriteFoods as an Array, without specifying the type (i.e. [String]). In that casefavoriteFoods defaults to Mixed type, and you have to manually mark it as edited using document.markModified('edited-field'). (http://mongoosejs.com/docs/schematypes.html - #Mixed )" ], "tests": [ { "text": "Find-edit-update an item should succeed", - "testString": "getUserInput => $.post(getUserInput('url') + '/_api/find-edit-save', {name:'Poldo', age: 40, favoriteFoods:['spaghetti']}).then(data => { assert.equal(data.name, 'Poldo', 'item.name is not what expected'); assert.equal(data.age, 40, 'item.age is not what expected'); assert.deepEqual(data.favoriteFoods, ['spaghetti', 'hamburger'], 'item.favoriteFoods is not what expected'); assert.equal(data.__v, 1, 'The item should be previously edited'); }, xhr => { throw new Error(xhr.responseText); })" + "testString": "getUserInput => $.post(getUserInput('url') +'/_api/find-edit-save', {name:'Poldo', age: 40, favoriteFoods:['spaghetti']}).then(data => { assert.equal(data.name,'Poldo','item.name is not what expected'); assert.equal(data.age, 40,'item.age is not what expected'); assert.deepEqual(data.favoriteFoods, ['spaghetti','hamburger'],'item.favoriteFoods is not what expected'); assert.equal(data.__v, 1,'The item should be previously edited'); }, xhr => { throw new Error(xhr.responseText); })" } ], "solutions": [], @@ -176,12 +173,12 @@ "description": [ "Recent versions of mongoose have methods to simplify documents updating. Some more advanced features (i.e. pre/post hooks, validation) behave differently with this approach, so the Classic method is still useful in many situations. findByIdAndUpdate() can be used when searching by Id.", "Find a person by Name and set her age to 20. Use the function parameter personName as search key.", - "Hint: We want you to return the updated document. To do that you need to pass the options document { new: true } as the 3rd argument to findOneAndUpdate(). By default these methods return the unmodified object." + "Hint: We want you to return the updated document. To do that you need to pass the options document {new: true} as the 3rd argument to findOneAndUpdate(). By default these methods return the unmodified object." ], "tests": [ { "text": "findOneAndUpdate an item should succeed", - "testString": "getUserInput => $.post(getUserInput('url') + '/_api/find-one-update', {name:'Dorian Gray', age: 35, favoriteFoods:['unknown']}).then(data => { assert.equal(data.name, 'Dorian Gray', 'item.name is not what expected'); assert.equal(data.age, 20, 'item.age is not what expected'); assert.deepEqual(data.favoriteFoods, ['unknown'], 'item.favoriteFoods is not what expected'); assert.equal(data.__v, 0, 'findOneAndUpdate does not increment version by design !!!'); }, xhr => { throw new Error(xhr.responseText); })" + "testString": "getUserInput => $.post(getUserInput('url') +'/_api/find-one-update', {name:'Dorian Gray', age: 35, favoriteFoods:['unknown']}).then(data => { assert.equal(data.name,'Dorian Gray','item.name is not what expected'); assert.equal(data.age, 20,'item.age is not what expected'); assert.deepEqual(data.favoriteFoods, ['unknown'],'item.favoriteFoods is not what expected'); assert.equal(data.__v, 0,'findOneAndUpdate does not increment version by design !!!'); }, xhr => { throw new Error(xhr.responseText); })" } ], "solutions": [], @@ -197,7 +194,7 @@ "tests": [ { "text": "Deleting an item should succeed", - "testString": "getUserInput => $.post(getUserInput('url') + '/_api/remove-one-person', {name:'Jason Bourne', age: 36, favoriteFoods:['apples']}).then(data => { assert.equal(data.name, 'Jason Bourne', 'item.name is not what expected'); assert.equal(data.age, 36, 'item.age is not what expected'); assert.deepEqual(data.favoriteFoods, ['apples'], 'item.favoriteFoods is not what expected'); assert.equal(data.__v, 0); assert.equal(data.count, 0, 'the db items count is not what expected'); }, xhr => { throw new Error(xhr.responseText); })" + "testString": "getUserInput => $.post(getUserInput('url') +'/_api/remove-one-person', {name:'Jason Bourne', age: 36, favoriteFoods:['apples']}).then(data => { assert.equal(data.name,'Jason Bourne','item.name is not what expected'); assert.equal(data.age, 36,'item.age is not what expected'); assert.deepEqual(data.favoriteFoods, ['apples'],'item.favoriteFoods is not what expected'); assert.equal(data.__v, 0); assert.equal(data.count, 0,'the db items count is not what expected'); }, xhr => { throw new Error(xhr.responseText); })" } ], "solutions": [], @@ -214,7 +211,7 @@ "tests": [ { "text": "Deleting many items at once should succeed", - "testString": "getUserInput => $.ajax({url: getUserInput('url') + '/_api/remove-many-people', type: 'POST', contentType:'application/json', data: JSON.stringify([{name: 'Mary', age: 16, favoriteFoods: ['lollipop']}, {name: 'Mary', age: 21, favoriteFoods: ['steak']}])}).then(data => { assert.isTrue(!!data.ok, 'The mongo stats are not what expected'); assert.equal(data.n, 2, 'The number of items affected is not what expected'); assert.equal(data.count, 0, 'the db items count is not what expected'); }, xhr => { throw new Error(xhr.responseText); })" + "testString": "getUserInput => $.ajax({url: getUserInput('url') +'/_api/remove-many-people', type:'POST', contentType:'application/json', data: JSON.stringify([{name:'Mary', age: 16, favoriteFoods: ['lollipop']}, {name:'Mary', age: 21, favoriteFoods: ['steak']}])}).then(data => { assert.isTrue(!!data.ok,'The mongo stats are not what expected'); assert.equal(data.n, 2,'The number of items affected is not what expected'); assert.equal(data.count, 0,'the db items count is not what expected'); }, xhr => { throw new Error(xhr.responseText); })" } ], "solutions": [], @@ -231,7 +228,7 @@ "tests": [ { "text": "Chaining query helpers should succeed", - "testString": "getUserInput => $.ajax({url: getUserInput('url') + '/_api/query-tools', type: 'POST', contentType:'application/json', data: JSON.stringify([{name: 'Pablo', age: 26, favoriteFoods: ['burrito', 'hot-dog']}, {name: 'Ashley', age: 32, favoriteFoods: ['steak', 'burrito']}, {name: 'Mario', age: 51, favoriteFoods: ['burrito', 'prosciutto']} ]) }).then(data => { assert.isArray(data, 'the response should be an Array'); assert.equal(data.length, 2, 'the data array length is not what expected'); assert.notProperty(data[0], 'age', 'The returned first item has too many properties'); assert.equal(data[0].name, 'Ashley', 'The returned first item name is not what expected'); assert.notProperty(data[1], 'age', 'The returned second item has too many properties'); assert.equal(data[1].name, 'Mario', 'The returned second item name is not what expected');}, xhr => { throw new Error(xhr.responseText); })" + "testString": "getUserInput => $.ajax({url: getUserInput('url') +'/_api/query-tools', type:'POST', contentType:'application/json', data: JSON.stringify([{name:'Pablo', age: 26, favoriteFoods: ['burrito','hot-dog']}, {name:'Ashley', age: 32, favoriteFoods: ['steak','burrito']}, {name:'Mario', age: 51, favoriteFoods: ['burrito','prosciutto']} ]) }).then(data => { assert.isArray(data,'the response should be an Array'); assert.equal(data.length, 2,'the data array length is not what expected'); assert.notProperty(data[0],'age','The returned first item has too many properties'); assert.equal(data[0].name,'Ashley','The returned first item name is not what expected'); assert.notProperty(data[1],'age','The returned second item has too many properties'); assert.equal(data[1].name,'Mario','The returned second item name is not what expected');}, xhr => { throw new Error(xhr.responseText); })" } ], "solutions": [], From 22c10436522aebd30aa5e227042cd6009c80906d Mon Sep 17 00:00:00 2001 From: S1ngS1ng Date: Sun, 29 Jul 2018 10:42:11 -0700 Subject: [PATCH 4/4] Fix style, revert testString changes --- .../mongodb-and-mongoose.json | 61 +++++++++---------- 1 file changed, 30 insertions(+), 31 deletions(-) diff --git a/05-apis-and-microservices/mongodb-and-mongoose.json b/05-apis-and-microservices/mongodb-and-mongoose.json index 71c649a..a7a0422 100644 --- a/05-apis-and-microservices/mongodb-and-mongoose.json +++ b/05-apis-and-microservices/mongodb-and-mongoose.json @@ -8,20 +8,20 @@ "id": "587d7fb6367417b2b2512c06", "title": "Install and Set Up Mongoose", "description": [ - "在 package.json 文件中添加 MongoDB 和 Mongoose 依赖,将 mLab 数据库的 URI 作为 MONGO_URI 变量存储在私有 .env 文件中。然后 require('mongoose'),使用 mongoose.connect() 命令来连接数据库。" + "在 package.json 文件中添加 MongoDB 和 Mongoose 依赖,将 mLab 数据库的 URI 作为 MONGO_URI 变量存储在私有 .env 文件中。然后require('mongoose'),使用mongoose.connect()命令来连接数据库。" ], "tests": [ { - "text": "在 package.json 文件中应该有 \"mongodb\"依赖。", - "testString": "getUserInput => $.get(getUserInput('url') +'/_api/file/package.json').then(data => { var packJson = JSON.parse(data); assert.property(packJson.dependencies,'mongodb'); }, xhr => { throw new Error(xhr.responseText); })" + "text": "在 package.json 文件中应该有 \"mongodb\" 依赖。", + "testString": "getUserInput => $.get(getUserInput('url') + '/_api/file/package.json').then(data => { var packJson = JSON.parse(data); assert.property(packJson.dependencies, 'mongodb'); }, xhr => { throw new Error(xhr.responseText); })" }, { - "text": "在 package.json 文件中应该有 \"mongoose\"依赖。", - "testString": "getUserInput => $.get(getUserInput('url') +'/_api/file/package.json').then(data => { var packJson = JSON.parse(data); assert.property(packJson.dependencies,'mongoose'); }, xhr => { throw new Error(xhr.responseText); })" + "text": "在 package.json 文件中应该有 \"mongoose\" 依赖。", + "testString": "getUserInput => $.get(getUserInput('url') + '/_api/file/package.json').then(data => { var packJson = JSON.parse(data); assert.property(packJson.dependencies, 'mongoose'); }, xhr => { throw new Error(xhr.responseText); })" }, { - "text": "\"mongoose\"应该已经连接数据库。", - "testString": "getUserInput => $.get(getUserInput('url') +'/_api/is-mongoose-ok').then(data => {assert.isTrue(data.isMongooseOk,'mongoose is not connected')}, xhr => { throw new Error(xhr.responseText); })" + "text": "\"mongoose\" 应该已经连接数据库。", + "testString": "getUserInput => $.get(getUserInput('url') + '/_api/is-mongoose-ok').then(data => {assert.isTrue(data.isMongooseOk, 'mongoose is not connected')}, xhr => { throw new Error(xhr.responseText); })" } ], "solutions": [], @@ -41,13 +41,13 @@ "name : string [required]", "age : number", "favoriteFoods : array of strings (*) ", - "你可以使用基础的 SchemaTypes 去添加更多的字段,比如使用 required 或者 unique 这样的简单验证去设置默认值。 参考 Mongoose 文档 。", + "你可以使用基础的 SchemaTypes 去添加更多的字段,比如使用 required 或者 unique 这样的简单验证去设置默认值。参考 Mongoose 文档。", "[C]RUD Part I - CREATE", - "注意: Glitch 是一个真实的服务,并且通过 handler 函数和 db 进行交互。 这些函数通过一些事件去触发 (例如:有人从终端调用了你的 API),我们在这些练习中遵循同样的方法。 比如,我们在完成 nserting、 searching、 updating 或者 deleting 这样的异步操作后接着回调 done() 函数。 它遵循 Node 的惯例,需要在 success 时回调 done(null, data), 在 error 时回调 done(err)。", + "注意: Glitch 是一个真实的服务,并且通过 handler 函数和 db 进行交互。 这些函数通过一些事件去触发(例如:有人从终端调用了你的 API),我们在这些练习中遵循同样的方法。 比如,我们在完成 nserting、searching、updating 或者 deleting 这样的异步操作后接着回调done()函数。它遵循 Node 的惯例,需要在 success 时回调done(null, data),在 error 时回调done(err)。", "Warning - 当与远程服务器交互时可能发生错误!", - "/* Example */", + "/* 示例 */", "var someFunc = function(done) {", - " //... do something (risky) ...", + " // 执行一些可能产生错误的代码", " if(error) return done(error);", " done(null, result);", "};" @@ -55,7 +55,7 @@ "tests": [ { "text": "成功创建一个 Schema 实例。", - "testString": "getUserInput => $.post(getUserInput('url') +'/_api/mongoose-model', {name:'Mike', age: 28, favoriteFoods: ['pizza','cheese']}).then(data => { assert.equal(data.name,'Mike','\"model.name\" is not what expected'); assert.equal(data.age,'28','\"model.age\" is not what expected'); assert.isArray(data.favoriteFoods,'\"model.favoriteFoods\" is not an Array'); assert.include(data.favoriteFoods,'pizza','\"model.favoriteFoods\" does not include the expected items'); assert.include(data.favoriteFoods,'cheese','\"model.favoriteFoods\" does not include the expected items'); }, xhr => { throw new Error(xhr.responseText); })" + "testString": "getUserInput => $.post(getUserInput('url') + '/_api/mongoose-model', {name: 'Mike', age: 28, favoriteFoods: ['pizza', 'cheese']}).then(data => { assert.equal(data.name, 'Mike', '\"model.name\" is not what expected'); assert.equal(data.age, '28', '\"model.age\" is not what expected'); assert.isArray(data.favoriteFoods, '\"model.favoriteFoods\" is not an Array'); assert.include(data.favoriteFoods, 'pizza', '\"model.favoriteFoods\" does not include the expected items'); assert.include(data.favoriteFoods, 'cheese', '\"model.favoriteFoods\" does not include the expected items'); }, xhr => { throw new Error(xhr.responseText); })" } ], "solutions": [], @@ -66,17 +66,17 @@ "id": "587d7fb6367417b2b2512c09", "title": "Create and Save a Record of a Model", "description": [ - "使用 Person 的 constructor(构造器) 函数可以创建一个 document 对象,该对象包含 nameagefavoriteFoods 字段。 这些字段的类型必须符合 Person Schema 里面定义的类型。 然后调用 document.save()。 使用 Node 惯例传递 callback。通常情况下,所有的 CRUD(增查改删) 方法都会像下面一样作为最后一个参数去执行一个 callback()。", - "/* Example */", + "使用 Person 的 constructor(构造器)函数可以创建一个 document 对象,该对象包含nameagefavoriteFoods字段。这些字段的类型必须符合 Person Schema 里面定义的类型。然后调用document.save()。使用 Node 惯例传递 callback。通常情况下,所有的 CRUD(增查改删)方法都会像下面一样作为最后一个参数去执行一个callback()。", + "/* 示例 */", "// ...", "person.save(function(err, data) {", - "// ...do your stuff here...", + "// 你的代码", "});" ], "tests": [ { "text": "成功创建一条 db 并保存。", - "testString": "getUserInput => $.get(getUserInput('url') +'/_api/create-and-save-person').then(data => { assert.isString(data.name,'\"item.name\" should be a String'); assert.isNumber(data.age,'28','\"item.age\" should be a Number'); assert.isArray(data.favoriteFoods,'\"item.favoriteFoods\" should be an Array'); assert.equal(data.__v, 0,'The db item should be not previously edited'); }, xhr => { throw new Error(xhr.responseText); })" + "testString": "getUserInput => $.get(getUserInput('url') + '/_api/create-and-save-person').then(data => { assert.isString(data.name, '\"item.name\" should be a String'); assert.isNumber(data.age, '28', '\"item.age\" should be a Number'); assert.isArray(data.favoriteFoods, '\"item.favoriteFoods\" should be an Array'); assert.equal(data.__v, 0, 'The db item should be not previously edited'); }, xhr => { throw new Error(xhr.responseText); })" } ], "solutions": [], @@ -87,13 +87,12 @@ "id": "587d7fb7367417b2b2512c0a", "title": "Create Many Records with model.create()", "description": [ - "有时你需要创建很多的 model 实例。 例如:在使用初始数据为数据库初始化时, Model.create() 接受一组像 [{name:'John', ...}, {...}, ...] 的数组作为第一个参数,并将其保存到数据库。使用 arrayOfPeople 作为 Model.create() 的参数创建很多个 people 实例。" + "有时你需要创建很多的 model 实例。例如:在使用初始数据为数据库初始化时,Model.create()接受一组像[{name:'John', ...}, {...}, ...]的数组作为第一个参数,并将其保存到数据库。使用arrayOfPeople作为Model.create()的参数创建很多个 people 实例。" ], "tests": [ { - "text": "Creating many db items at once should succeed", - "text": "一次性创建多个 item(项目)。", - "testString": "getUserInput => $.ajax({url: getUserInput('url') +'/_api/create-many-people', type:'POST', contentType:'application/json', data: JSON.stringify([{name:'John', age: 24, favoriteFoods: ['pizza','salad']}, {name:'Mary', age: 21, favoriteFoods: ['onions','chicken']}])}).then(data => { assert.isArray(data,'the response should be an array'); assert.equal(data.length, 2,'the response does not contain the expected number of items'); assert.equal(data[0].name,'John','The first item is not correct'); assert.equal(data[0].__v, 0,'The first item should be not previously edited'); assert.equal(data[1].name,'Mary','The second item is not correct'); assert.equal(data[1].__v, 0,'The second item should be not previously edited'); }, xhr => { throw new Error(xhr.responseText); })" + "text": "应当可以一次性创建多个 item(项目)。", + "testString": "getUserInput => $.ajax({url: getUserInput('url') + '/_api/create-many-people', type: 'POST', contentType:'application/json', data: JSON.stringify([{name: 'John', age: 24, favoriteFoods: ['pizza', 'salad']}, {name: 'Mary', age: 21, favoriteFoods: ['onions', 'chicken']}])}).then(data => { assert.isArray(data, 'the response should be an array'); assert.equal(data.length, 2, 'the response does not contain the expected number of items'); assert.equal(data[0].name, 'John', 'The first item is not correct'); assert.equal(data[0].__v, 0, 'The first item should be not previously edited'); assert.equal(data[1].name, 'Mary', 'The second item is not correct'); assert.equal(data[1].__v, 0, 'The second item should be not previously edited'); }, xhr => { throw new Error(xhr.responseText); })" } ], "solutions": [], @@ -104,13 +103,13 @@ "id": "587d7fb7367417b2b2512c0b", "title": "Use model.find() to Search Your Database", "description": [ - "使用 Model.find() -> [Person] 来查询给定名称的所有的人。", - "最简单的用法: Model.find() 接受一个查询的 document (一个 JSON 对象 ) 作为第一参数, 然后是回调。 它将返回匹配到的项目组成的数组。 这个支持极其广泛的搜索选项。 使用人名作为搜索的关键词, 来校验它。" + "使用Model.find() -> [Person]来查询给定名称的所有的人。", + "最简单的用法:Model.find()接受一个查询的 document(一个 JSON 对象)作为第一参数,然后是回调。它将返回匹配到的项目组成的数组。这个支持极其广泛的搜索选项。使用人名作为搜索的关键词,来校验它。" ], "tests": [ { - "text": "成功找到所有符合条件的 item(项目)。", - "testString": "getUserInput => $.post(getUserInput('url') +'/_api/find-all-by-name', {name:'r@nd0mN4m3', age: 24, favoriteFoods: ['pizza']}).then(data => { assert.isArray(data,'the response should be an Array'); assert.equal(data[0].name,'r@nd0mN4m3','item.name is not what expected'); assert.equal(data[0].__v, 0,'The item should be not previously edited'); }, xhr => { throw new Error(xhr.responseText); })" + "text": "成功找到所有符合条件的 item(项目)。", + "testString": "getUserInput => $.post(getUserInput('url') + '/_api/find-all-by-name', {name: 'r@nd0mN4m3', age: 24, favoriteFoods: ['pizza']}).then(data => { assert.isArray(data, 'the response should be an Array'); assert.equal(data[0].name, 'r@nd0mN4m3', 'item.name is not what expected'); assert.equal(data[0].__v, 0, 'The item should be not previously edited'); }, xhr => { throw new Error(xhr.responseText); })" } ], "solutions": [], @@ -121,12 +120,12 @@ "id": "587d7fb7367417b2b2512c0c", "title": "Use model.findOne() to Return a Single Matching Document from Your Database", "description": [ - "Model.findOne() 表现像 Model.find(), 但是它仅仅返回一个 document (而不是一个数组),即使数据库里有很多条 item(项目)。 当你按声明成 unique 的属性进行搜索时,Model.findOne() 尤其有用。 把食物作为 Model.findOne() -> Person 的参数,来找到一个在她的爱好中有某一食物的人。" + "Model.findOne()表现像Model.find(),但是它仅仅返回一个 document(而不是一个数组),即使数据库里有很多条 item(项目)。当你按声明成unique的属性进行搜索时,Model.findOne()尤其有用。把食物作为Model.findOne() -> Person的参数,来找到一个在她的爱好中有某一食物的人。" ], "tests": [ { - "text": "成功找到一个 item(项目)。", - "testString": "getUserInput => $.post(getUserInput('url') +'/_api/find-one-by-food', {name:'Gary', age: 46, favoriteFoods: ['chicken salad']}).then(data => { assert.equal(data.name,'Gary','item.name is not what expected'); assert.deepEqual(data.favoriteFoods, ['chicken salad'],'item.favoriteFoods is not what expected'); assert.equal(data.__v, 0,'The item should be not previously edited'); }, xhr => { throw new Error(xhr.responseText); })" + "text": "成功找到一个 item(项目)。", + "testString": "getUserInput => $.post(getUserInput('url') + '/_api/find-one-by-food', {name: 'Gary', age: 46, favoriteFoods: ['chicken salad']}).then(data => { assert.equal(data.name, 'Gary', 'item.name is not what expected'); assert.deepEqual(data.favoriteFoods, ['chicken salad'], 'item.favoriteFoods is not what expected'); assert.equal(data.__v, 0, 'The item should be not previously edited'); }, xhr => { throw new Error(xhr.responseText); })" } ], "solutions": [], @@ -137,12 +136,12 @@ "id": "587d7fb7367417b2b2512c0d", "title": "Use model.findById() to Search Your Database By _id", "description": [ - "当我们保存一个 document, MongoDB 自动添加 _id 字段, 并给该字段设置 unique(唯一) 属性。通过 _id 搜索是一个非常频繁的操作,所以 Mongose 为它提供了一个专门的方法。使用人物 Id 作为参数,执行 Model.findById() -> Person, 找到这个_id 对应的唯一的一个人。" + "当我们保存一个 document, MongoDB 自动添加 _id 字段,并给该字段设置 unique(唯一)属性。通过 _id 搜索是一个非常频繁的操作,所以 Mongose 为它提供了一个专门的方法。使用人物 Id 作为参数,执行Model.findById() -> Person,找到这个 _id 对应的唯一的一个人。" ], "tests": [ { - "text": "通过 Id 成功找到对应的 item(项目)。", - "testString": "getUserInput => $.get(getUserInput('url') +'/_api/find-by-id').then(data => { assert.equal(data.name,'test','item.name is not what expected'); assert.equal(data.age, 0,'item.age is not what expected'); assert.deepEqual(data.favoriteFoods, ['none'],'item.favoriteFoods is not what expected'); assert.equal(data.__v, 0,'The item should be not previously edited'); }, xhr => { throw new Error(xhr.responseText); })" + "text": "通过 Id 成功找到对应的 item(项目)。", + "testString": "getUserInput => $.get(getUserInput('url') + '/_api/find-by-id').then(data => { assert.equal(data.name, 'test', 'item.name is not what expected'); assert.equal(data.age, 0, 'item.age is not what expected'); assert.deepEqual(data.favoriteFoods, ['none'], 'item.favoriteFoods is not what expected'); assert.equal(data.__v, 0, 'The item should be not previously edited'); }, xhr => { throw new Error(xhr.responseText); })" } ], "solutions": [], @@ -236,4 +235,4 @@ "challengeType": 2 } ] -} \ No newline at end of file +}