【发布时间】:2016-10-13 21:23:58
【问题描述】:
所以,我有一个架构:
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: 'client.a@thejump.tech',
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);
...数据不一样(注意,此时未保存):
{
email: 'client.a@thejump.tech',
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'
}
当它出现故障时:
- 擦除
tab_access数据 - 从部门中删除
entityid并将其替换为 _id
有人知道为什么吗?任何帮助将不胜感激...
****编辑****
所以,为了清楚起见,我的路由处理程序如下所示:
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);
});
}
其中management 是 auto0 管理客户端。我重新排列数据以制作storedUser,然后通过在new User(storedUser) 中传递该数据来制作新模型并尝试保存模型。在保存模型之前,我注销了已生成的内容并看到了上述问题。
我展示的两个对象是它产生的storedUser(post) 和mongoUser。
【问题讨论】:
-
您可以编辑问题并添加您执行的查询以插入文档吗?
-
@Svabael 在这个阶段我还没有将它保存到 mongo。我做
mongoUser.save()下一个... -
mongoUser.save 会将文档保存在 mongo 中
-
只是数组没有保存吗?
标签: node.js mongodb mongoose mongoose-schema