データを変換しないで、私は、スキーマ持っている:マングーススキーマだからきちんと
import mongoose from 'mongoose';
import cuid from 'cuid';
import timestamps from 'mongoose-timestamp';
import validators from 'mongoose-validators';
const Schema = mongoose.Schema;
const userSchema = new Schema({
auth0id: { type: 'String', required: true },
cuid: { type: 'String', default: cuid(), required: true },
dateAdded: { type: 'Date', default: Date.now, required: true },
email: { type: 'String', required: true, validate: validators.isEmail() },
avatarURL: { type: 'String', required: true, validate: validators.isURL() },
username: { type: 'String' },
accessLevel: { type: 'Number', required: true },
entities: [{ type: 'String', required: true }],
preferredEntity: { type: 'String', required: true },
tab_access: [{
entityid: { type: 'String', required: true },
tabs: [{ type: 'String' }],
}],
departments: [{
entitytid: { type: 'String', required: true },
deptid: { type: 'String', required: true },
}],
});
userSchema.plugin(timestamps);
export default mongoose.model('User', userSchema);
をし、私は私が処理されてきたいくつかのデータ持っている:私は作成すると
{
email: '[email protected]',
username: 'Fred Bloggs',
accessLevel: 5,
entities: ['cit98hg1j000578jcffur7c6h'],
auth0id: 'auth0|57ff79140dd70f9616b80a9d',
avatarURL: 'https://s.gravatar.com/avatar/2dfc6cb16eea798886af841e7b1ee8ab?s=480&r=pg&d=https%3A%2F%2Fcdn.auth0.com%2Favatars%2Ffb.png',
preferredEntity: 'cit98hg1j000578jcffur7c6h',
departments: [{
entityid: 'cit98hg1j000578jcffur7c6h',
deptid: 'cit98hg1j000572jcffur7c6h'
}],
tabAccess: [{
entityid: 'cit98hg1j000578jcffur7c6h',
tabs: []
}]
}
を新しいモデル:
const mongoUser = new User(userData);
...データは(NBこの時点で保存されていない)と同じではありません。
{
email: '[email protected]',
username: 'Fred Bloggs',
accessLevel: 5,
auth0id: 'auth0|57ff79140dd70f9616b80a9d',
avatarURL: 'https://s.gravatar.com/avatar/2dfc6cb16eea798886af841e7b1ee8ab?s=480&r=pg&d=https%3A%2F%2Fcdn.auth0.com%2Favatars%2Ffb.png',
preferredEntity: 'cit98hg1j000578jcffur7c6h',
_id: 57 ff791473da244be87f0489,
departments: [{
deptid: 'cit98hg1j000572jcffur7c6h',
_id: 57 ff791473da244be87f048a
}],
tab_access: [],
entities: ['cit98hg1j000578jcffur7c6h'],
dateAdded: Thu Oct 13 2016 13: 07: 48 GMT + 0100(BST),
cuid: 'ciu8aq0430000k7jck2zsi5zm'
}
それフォルトその際:
- は
- は部署から
entityid
を削除し、_id
に置き換えて、誰もがなぜ知っているか?tab_access
データをワイプすべてのヘルプは本当にいただければ幸いです...
**** **** EDIT
だから、ちょうど明確にすることが、私のルートハンドラは次のようになります。
management
がある
export function addUser(req, res) {
const sentUser = req.body.user;
console.log('sentUser', sentUser);
const newUser = {
connection: process.env.AUTH0_DB_CONNECTION,
password: process.env.AUTH0_DEFAULT_PASSWORD,
username: sentUser.username,
nickname: sentUser.username,
email: sentUser.email,
email_verified: false,
app_metadata: {
accessLevel: sentUser.accessLevel,
},
};
console.log('newUser', newUser);
management.createUser(newUser).then(user => {
console.log('user returned on auth0 creation: ', user);
const storedUser = { ...req.body.user };
console.log('storedUser pre', storedUser);
storedUser.auth0id = user.user_id;
storedUser.avatarURL = user.picture;
storedUser.preferredEntity = storedUser.entities[0].entityid;
storedUser.departments = storedUser.entities.map((ent) => {
return {
entityid: ent.entityid,
deptid: ent.deptid,
};
});
storedUser.tabAccess = storedUser.entities.map((ent) => {
return {
entityid: ent.entityid,
tabs: [],
};
});
storedUser.entities = storedUser.entities.map((ent) => {
return ent.entityid;
});
console.log('storedUser post', storedUser);
const mongoUser = new User(storedUser);
console.log('mongoUser', mongoUser);
mongoUser.save((err, saved) => {
if (err) {
console.log('problem saving', err);
return res.status(500).send(err);
}
return res.status(201).send({ user: saved });
});
}).catch(err => {
console.log('problem saving to Auth0')
return res.status(500).send(err);
});
}
auto0管理クライアント。データを並べ替えてstoredUser
とし、そのデータをnew User(storedUser)
に渡して新しいモデルを作成し、モデルを保存しようとします。モデルを保存する前に、私は生産されたものをログアウトし、上記の問題を見た。
私が示す2つのオブジェクトは、storedUser
(投稿)とそれが生成するmongoUser
です。
質問を編集し、ドキュメントを挿入するために実行するクエリを追加できますか? –
@Svabaelこの段階では、私はそれをmongoに保存していません。私は 'mongoUser.save()'を次に実行します... – user1775718
mongoUser.saveは文書をmongoに保存します –