1.7 KiB
Raw Blame History

id, title, challengeType, isHidden, localeTitle
id title challengeType isHidden localeTitle
587d7fb6367417b2b2512c09 Create and Save a Record of a Model 2 false 用模型来创建并保存一条记录

Description

在本挑战中创建保存一条记录。

Instructions

使用 Person 的 constructor构造器函数可以创建一个 document 对象,该对象包含nameagefavoriteFoods字段。这些字段的类型必须符合 Person Schema 里面定义的类型。然后调用document.save()。使用 Node 惯例传递 callback。通常情况下所有的 CRUD增查改删方法都会像下面一样作为最后一个参数去执行一个callback()
/* Example */

// ...
person.save(function(err, data) {
  //   ...do your stuff here...
});

Tests

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); })'

Challenge Seed

Solution

/**
  Backend challenges don't need solutions, 
  because they would need to be tested against a full working project. 
  Please check our contributing guidelines to learn more.
*/