4.0 KiB
4.0 KiB
id | title | challengeType | videoUrl | localeTitle |
---|---|---|---|---|
5895f70cf9fc0f352b528e66 | Serialization of a User Object | 2 | 用户对象的序列化 |
Description
passport.serializeUser( OURFUNCTION )
和passport.deserializeUser( OURFUNCTION )
创建它们。使用2个参数调用serializeUser,完整的用户对象和护照使用的回调。在回调中返回应该是唯一的键来标识该用户 - 最容易使用的用户是对象中的用户_id,因为它应该是MongoDb生成的唯一用户。类似地,使用该密钥和护照的回调函数调用deserializeUser,但这次我们必须获取该密钥并将用户完整对象返回到回调。要进行查询搜索Mongo _id,您必须创建const ObjectID = require('mongodb').ObjectID;
,然后使用它调用new ObjectID(THE_ID)
。一定要将MongoDB添加为依赖项。您可以在以下示例中看到: passport.serializeUser((user,done)=> { done(null,user._id); });
passport.deserializeUser((id,done)=> { db.collection( '用户')。findOne( {_id:new ObjectID(id)}, (错误,doc)=> { 完成(null,doc); } ); });注意:这个deserializeUser将抛出一个错误,直到我们在下一步中设置数据库,因此注释掉整个块并在函数deserializeUser中调用
done(null, null)
。当您认为自己已经做对时,请提交您的页面。 Instructions
Tests
tests:
- text: 序列化用户功能正确
testString: 'getUserInput => $.get(getUserInput("url")+ "/_api/server.js") .then(data => { assert.match(data, /passport.serializeUser/gi, "You should have created your passport.serializeUser function"); assert.match(data, /null, user._id/gi, "There should be a callback in your serializeUser with (null, user._id)"); }, xhr => { throw new Error(xhr.statusText); })'
- text: 反序列化用户功能正确
testString: 'getUserInput => $.get(getUserInput("url")+ "/_api/server.js") .then(data => { assert.match(data, /passport.deserializeUser/gi, "You should have created your passport.deserializeUser function"); assert.match(data, /null,( |)null/gi, "There should be a callback in your deserializeUser with (null, null) for now"); }, xhr => { throw new Error(xhr.statusText); })'
- text: MongoDB是一个依赖项
testString: 'getUserInput => $.get(getUserInput("url")+ "/_api/package.json") .then(data => { var packJson = JSON.parse(data); assert.property(packJson.dependencies, "mongodb", "Your project should list "mongodb" as a dependency"); }, xhr => { throw new Error(xhr.statusText); })'
- text: Mongodb正确要求包括ObjectId
testString: 'getUserInput => $.get(getUserInput("url")+ "/_api/server.js") .then(data => { assert.match(data, /require.*("|")mongodb("|")/gi, "You should have required mongodb"); assert.match(data, /new ObjectID.*id/gi, "Even though the block is commented out, you should use new ObjectID(id) for when we add the database"); }, xhr => { throw new Error(xhr.statusText); })'
Challenge Seed
Solution
// solution required