user1775718 user1775718 - 1 month ago 14
Node.js Question

Mongoose Schema not converting data properly

So, I've a got a schema:

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


and I've got some data that I've processed:

{
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: []
}]
}


When I create a new model:

const mongoUser = new User(userData);


...the data is not the same (N.B. Not saved at this point):

{
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'
}


It faults when it:


  1. Wipes the
    tab_access
    data

  2. Removes the
    entityid
    from departments and replaces it with _id



Does anyone know why?? Any help would be really appreciated...

****EDIT****

So, just to be clear, my route handler looks like this:

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


where
management
is the auto0 management client. I rearrange the data to make
storedUser
and then I make a new model by passing that data in the
new User(storedUser)
and attempt to save the model. Before saving the model I logged out what had been produced and saw the issues noted above.

The two objects I show are the
storedUser
(post) and the
mongoUser
that it produces.

Answer

Reason is wrong key name:

departments: [{
    entitytid: // change it in schema to "entityid"

notice the spelling of "entitytid". it should be "entityid".

Second, tab_access and tabAccess. you have tab_access in schema but you are passing tabAccess at the time of creating object.

correct this key while you are creating "storedUser".