freeCodeCamp/curriculum/challenges/chinese/05-apis-and-microservices/mongodb-and-mongoose/create-a-model.chinese.md

2.7 KiB
Raw Blame History

id title localeTitle challengeType
587d7fb6367417b2b2512c07 Create a Model 创建一个模型 2

Description

0首先我们需要一个Schema。每个模式都映射到MongoDB集合。它定义了该集合中文档的形状。 0模式是模型的构建块。它们可以嵌套来创建复杂的模型,但在这种情况下,我们会保持简单。 0模型允许您创建对象的实例,称为文档。 0创建一个拥有此原型的人: - Person Prototype - -------------------- name : string [required] age : number favoriteFoods : array of strings (*) 0使用mongoose基本模式类型。如果需要还可以添加0个字段使用简单的验证器如required或unique 0并设置默认值。请参阅mongoose文档 。 [C] RUD第一部分 - 创建0注意Glitch是一个真实的服务器在真实服务器中与db的交互发生在处理函数中。当某些事件发生时执行这些功能例如某人在您的API上命中端点。我们将在这些练习中遵循相同的方法。 done函数是一个回调告诉我们在完成插入搜索更新或删除等异步操作后我们可以继续。它遵循Node约定应该在成功时调用donenulldata或者在出错时调用err0警告 - 与远程服务交互时,可能会发生错误! /* Example */ var someFunc = function(done) { //... do something (risky) ... if(error) return done(error); done(null, result); };

Instructions

Tests

tests:
  - text: 从mongoose模式创建实例应该会成功
    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); })'

Challenge Seed

Solution

// solution required