Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

model.save() saved req.body in toString format having special characters in each key #13

Closed
rockamitk opened this issue Nov 16, 2017 · 3 comments

Comments

@rockamitk
Copy link

//Schema Def
const schema = new mongoose.Schema({ name: {type: String, alias:'enquiry_name', required: true, minLength: 3, maxLength: 30}, email: {type: String, alias:'enquiry_email', required: true}, }); schema.plugin(require('mongoose-aliasfield'));

//Model Def
User = module.exports = mongoose.model('User' ,schema);

let user = new User({
enquiry_name: req.body.enquiry_name,
enquiry_email: req.body.enquiry_email
});
user.save()

/Stores in Db/
{ "_id" : ObjectId("5a0d70418caef3234092fd2e"), "name" : "{ name: 'raj',\n email: '{ email: \'raj@aviota.ai\',_id: 5a0d70418caef3234092fd2e,}\',\n _id: 5a0d70418caef3234092fd2e}',"email" : "{ email: 'raj@aviota.ai',\n _id: 5a0d70418caef3234092fd2e,\n}',\n _id: 5a0d70418caef3234092fd2e}"}

What actually happening here??

@ramiel
Copy link
Owner

ramiel commented Nov 16, 2017

It looks like req.body.enquiry_name contains an object instead of a string.
Try to write

let user = new User({
enquiry_name: 'a name',
enquiry_email: 'an@email.com'
});
user.save()

and see what happens

@rockamitk
Copy link
Author

It was mongoose version issues. I was using mongoose 4.10.6.

Is there any create alias of _id?

@ramiel
Copy link
Owner

ramiel commented Nov 17, 2017

I never tried honestly, but _id is already aliased by mongoose as id, maybe it's enough.

@ramiel ramiel closed this as completed Nov 17, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants