Related
I'm learning to use Node.js and MongoDB. I have a problem when I try to save data to the database.
Here's my code
const Test = require('../models/test');
const test = (req, res, next) => {
let url = "http://localhost:3000/article"
request(url, (req, (err, fields) => {
if (err) {
return res.status(400).json({
error: "error"
})
}
var objTest = JSON.parse(fields.body);
console.log(objTest.user)
let test = new Test(objTest)
console.log("ini",test)
test
.save()
.then(result => {
res.send(result)
})
.catch(err => {
console.log(err);
res.status(500).json({
error: err
});
});
}))
}
here my Test Schema
const testSchema = new mongoose.Schema(
{
tittle: {
type: String,
index: true,
},
content: {
type: String,
},
postedBy: { type: mongoose.Schema.ObjectId, ref: 'Author' },
created: {
type: Date,
default: Date.now,
},
},
{
timestamps: true,
},
);
module.exports = mongoose.model('Test', testSchema, 'tests');
The response in Postman is only id, createdAt and updatedAt. Thank you.
I want to use more than one schema for a single collection in mongodb but it doesn't stores except single schema. other schema only stores _id & _v what is wrong in my code ?
I have tried to follow
http://mongoosejs.com/docs/api.html#index_Mongoose-model
My schema model
const mongoose = require('mongoose');
const config = require('../config/database');
var Schema = mongoose.Schema;
// Reading content Schema
const contentArticle = new Schema({
contentCategory: { type: String },
contentTitle: { type: String },
body: { type: String },
});
// Quiz Content
const quizAllArticle = new Schema({
quizCategory: { type: String },
question: { type: String },
ans: { type: String }
});
// Article Schema
const topicSchema = new Schema({
artId: { type: String },
postURL: { type: String },
date: { type: Date, default: Date.now },
author: { type: String },
published: {type: String },
category: { type: String },
QuesPapType :{ type: String },
});
const Content = module.exports = mongoose.model('Contents', contentArticle, 'articles');
const Quiz = module.exports = mongoose.model('Quizs', quizAllArticle, 'articles');
const Topic = module.exports = mongoose.model('TopicContent', topicSchema, 'articles');
and other code is :
const express = require('express');
const router = express.Router();
const passport = require('passport');
const config = require('../config/database');
const Content = require('../models/art');
const Quiz = require('../models/art');
const Topic = require('../models/art');
router.post('/register',(req, res, next) => {
let topic= new Topic({
artId : req.body.artId,
author : req.body.author,
date : req.body.date,
published : req.body.published,
postURL : req.body.postURL,
category : req.body.pscCategory,
QuesPapType : req.body.QuesPapType,
});
console.log(topic);
topic.save(function(err) {
if (err) {
res.json({
success: false,
msg: 'failed to register'
})
} else {
res.json({
success: true,
msg: 'success to reg article'
})
}
})
});
router.post('/reg-content', (req,res,next) => {
let content= new Content({
contentCategory : req.body.contentCategory,
contentTitle : req.body.contentTitle,
body : req.body.body,
});
console.log(content);
content(function(err) {
if (err) {
res.json({success: false, msg: 'failed to register Content'})
} else {
res.json({success: true,msg: 'success to reg article'})
}
})
})
router.post('/reg-quiz', (req,res,next) => {
let quiz= new Quiz({
quizCategory : req.body.quizCategory,
question : req.body.question,
ans : req.body.ans,
});
console.log(quiz);
quiz.save(function(err) {
if (err) {
res.json({success: false,msg: 'failed to register Content'})
} else {
res.json({ success: true, msg: 'success to reg article'})
}
})
})
module.exports = router;
The main problem is that it saves to database from only one schema and other two schema saves only _id, date and _v
/* --------------------- models/art.js ------------------------------------*/
const mongoose = require('mongoose');
var Schema = mongoose.Schema;
// Reading content Schema
const contentArticle = new Schema({
contentCategory: { type: String },
contentTitle: { type: String },
body: { type: String },
});
// Quiz Content
const quizAllArticle = new Schema({
quizCategory: { type: String },
question: { type: String },
ans: { type: String }
});
// Article Schema
const topicSchema = new Schema({
artId: { type: String },
postURL: { type: String },
date: { type: Date, default: Date.now },
author: { type: String },
published: {type: String },
category: { type: String },
QuesPapType :{ type: String },
});
const Content = mongoose.model('Contents', contentArticle);
const Quiz = mongoose.model('Quizs', quizAllArticle);
const Topic = mongoose.model('TopicContent', topicSchema);
module.exports = {Content, Quiz, Topic};
/*--------------------routes/index.js ------------------------------------*/
var express = require('express');
var router = express.Router();
const {Content, Quiz, Topic} = require('../models/art.js');
/* GET home page. */
router.get('/', function(req, res, next) {
res.render('index', { title: 'Express' });
});
router.post('/register',function(req, res, next) {
let topic= new Topic({
artId : req.body.artId,
author : req.body.author,
date : req.body.date,
published : req.body.published,
postURL : req.body.postURL,
category : req.body.pscCategory,
QuesPapType : req.body.QuesPapType,
});
console.log(topic);
topic.save(function(err) {
if (err) {
res.json({
success: false,
msg: 'failed to register'
})
} else {
res.json({
success: true,
msg: 'success to reg article'
})
}
});
});
router.post('/reg-content', function(req,res,next){
let content= new Content({
contentCategory : req.body.contentCategory,
contentTitle : req.body.contentTitle,
body : req.body.body,
});
console.log(content);
content(function(err) {
if (err) {
res.json({success: false, msg: 'failed to register Content'})
} else {
res.json({success: true,msg: 'success to reg article'})
}
})
});
router.post('/reg-quiz', function(req,res,next){
let quiz= new Quiz({
quizCategory : req.body.quizCategory,
question : req.body.question,
ans : req.body.ans,
});
console.log(quiz);
quiz.save(function(err) {
if (err) {
res.json({success: false,msg: 'failed to register Content'})
} else {
res.json({ success: true, msg: 'success to reg article'})
}
})
});
module.exports = router;
const Content = mongoose.model('Contents', contentArticle);
const Quiz = mongoose.model('Quizs', quizAllArticle);
const Topic = mongoose.model('TopicContent', topicSchema);
module.exports = {Content, Quiz, Topic};
//import
const {Content, Quiz, Topic} = require('your schema file');
I want to make the key email unique across that collection but i cant getting this working, here is my server code.
// Create a schema
var userSchema = new mongoose.Schema({
email: { type: String, required: true},
password: String
});
var userModel = mongoose.model("user", userSchema);
router.post('/postuser', (req, res) => {
console.log('Requested data to server: ' + JSON.stringify(req.body._user));
var user = new userModel({
email: req.body._user.email,
password: req.body._user.password
});
// user.isNew = false;
user.save((err, data) => {
console.log('Analyzing Data...');
if(data) {
console.log('Your data has been successfully saved.');
res.json(data);
}
else {
console.log('Something went wrong while saving data.');
console.log(err);
res.send(err);
}
})
});
Note: I also try email: { type: String, required: true, unique: true} but its not working and show below error.
name: 'MongoError',
message: 'E11000 duplicate key error collection: hutreservationsystem.users
index: _Email_1 dup key: { : null }',
driver: true,
code: 11000,
index: 0,
errmsg: 'E11000 duplicate key error collection: hutreservationsystem.users index: _Email_1 dup key: { : null }',
getOperation: [Function],
toJSON: [Function],
toString: [Function] }
A short answer using this tool mongoose-unique-validator
npm install --save mongoose-unique-validator
and in your model
var mongoose = require('mongoose')
var uniqueValidator = require('mongoose-unique-validator')
var userSchema = new mongoose.Schema({
email: { type: String, required: true, unique: true},
password: String
});
userSchema.plugin(uniqueValidator)
var userModel = mongoose.model("user", userSchema);
That's it! (Notice unique: true)
Now, there is no email duplication in your collection.
Bonus! : you can access err
.catch(err => console.log(err))
so in your example
// user.isNew = false;
user.save((err, data) => {
console.log('Analyzing Data...');
if(data) {
console.log('Your data has been successfully saved.');
res.json(data);
}
else {
console.log('Something went wrong while saving data.');
console.log(err);
res.send(err);
}
accessing err >>
so you can res.send(err.message) >> 'Validation failed'
{
message: 'Validation failed',
name: 'ValidationError',
errors: {
email: {
message: 'Error, expected `email` to be unique. Value: `example#gmail.com`',
name: 'ValidatorError',
kind: 'unique',
path: 'email',
value: 'example#gmail.com'
}
}
}
Async Custom Validator
var userSchema = new mongoose.Schema({
password: String,
email: {
type: String,
lowercase: true,
required: true,
validate: {
isAsync: true,
validator: function(value, isValid) {
const self = this;
return self.constructor.findOne({ email: value })
.exec(function(err, user){
if(err){
throw err;
}
else if(user) {
if(self.id === user.id) { // if finding and saving then it's valid even for existing email
return isValid(true);
}
return isValid(false);
}
else{
return isValid(true);
}
})
},
message: 'The email address is already taken!'
},
}
});
You may like to change the validator code to es6.
email: {
type: String,
trim: true,
unique: true, // note - this is a unqiue index - not a validation
validate: {
validator: function(value) {
const self = this;
const errorMsg = 'Email already in use!';
return new Promise((resolve, reject) => {
self.constructor.findOne({ email: value })
.then(model => model._id ? reject(new Error(errorMsg)) : resolve(true)) // if _id found then email already in use
.catch(err => resolve(true)) // make sure to check for db errors here
});
},
}
},
I implemented the following code to see if anything was wrong:
var mongoose = require('mongoose');
var bodyParser = require('body-parser');
var express = require('express');
var http = require('http');
var app = express();
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: false }));
var Schema = mongoose.Schema;
mongoose.connect('mongodb://localhost/test');
// Create a schema
var userSchema = new mongoose.Schema({
email: { type: String, required: true, unique: true},
password: String
});
var userModel = mongoose.model("user", userSchema);
app.post('/postuser', (req, res) => {
console.log('Requested data to server: ' + JSON.stringify(req.body._user));
var user = new userModel({
email: req.body._user.email,
password: req.body._user.password
});
// user.isNew = false;
user.save((err, data) => {
console.log('Analyzing Data...');
if(data) {
console.log('Your data has been successfully saved.');
res.json(data);
}
else {
console.log('Something went wrong while saving data.');
console.log(err);
res.send(err);
}
})
});
http.createServer(app).listen(3000, function(){
console.log('Express server listening on port 3000');
});
And I made sure that no collection by the name of users existed in my local MongoDB database. Moreover, I used Postman for sending API requests to my server at http://localhost:3000. There seemed to be no issue as I continued to add users with differing email values. And I only got the following error when I entered an email with a duplicate value
{
"code": 11000,
"index": 0,
"errmsg": "E11000 duplicate key error collection: test.users index: email_1 dup key: { : \"hot#mail.com\" }",
"op": {
"email": "hot#mail.com",
"password": "1234567",
"_id": "5919a3428c13271f6f6eab0f",
"__v": 0
}
}
These are the JSON requests that I sent:
{"_user": {"email": "hot#mail.com", "password": "1234"}}
{"_user": {"email": "sammy#mail.com", "password": "1234"}}
{"_user": {"email": "tommy#mail.com", "password": "1234"}}
{"_user": {"email": "tommy#mail.ae", "password": "1234567"}}
{"_user": {"email": "hot#mail.com", "password": "1234567"}}
The error mentioned above was sent back on the last request as the email hot#mail.com is repeated.
If you view the link http://mongoosejs.com/docs/api.html#schematype_SchemaType-unique , you'll see that the E11000 error is only sent when the email entered is not unique. Moreover, your email can't be an empty string or not be present as that violates the required property.
ES6:
const userSchema = new Schema({
name: String,
id: {
type: String,
required: true,
unique: true,
validate: async (value) => {
try {
const result = await userModel.findOne({ id: value })
if (result) throw new Error("duplicity detected: id :" + value);
} catch (error) {
throw new Error(error);
}
}
}
})
const userModel = mongoose.model<Document>('users', userSchema);
ES6 (TypeScript):
const userSchema = new Schema({
name: String,
id: {
type: String,
required: true,
unique: true,
validate: async (value: any): Promise<any> => {
try {
const result: Document | null = await userModel.findOne({ id: value })
if (result) throw new Error("duplicity detected: id :" + value);
} catch (error) {
throw new Error(error);
}
}
}
})
const userModel: Model<Document, {}> = mongoose.model<Document>('users', userSchema);
In your user schema set attribute email as unique (unique: true).
var userSchema = new mongoose.Schema({ email: { type: String, required: true, unique: true}, });
I'm connecting a Angular 2 app to MongoDB via Mongoose.
I'm trying to store some data, but i obtain an error on all required properties.
I set up a schema, serverside:
var mongoose = require("mongoose");
var Schema = mongoose.Schema;
var uniqueValidator = require("mongoose-unique-validator");
var schema = new Schema({
email: {
type: String,
required: true,
unique: true
},
password: {
type: String,
required: true
},
nome: {
type: String,
required: true
},
cognome: {
type: String,
required: true
},
dataNascita: {
type: Date
},
telefono: {
type: String
},
classifica: {
type: String
}
});
schema.plugin(uniqueValidator);
module.exports = mongoose.model("User", schema);
The user object is clearly filled:
Mongoose responds with an error:
Thanks in advance for any help.
Max
Update:
The call from a Angular service:
#Injectable()
export class AuthService {
constructor(private http: Http) {
}
addUser(utente: Utente) {
const body = JSON.stringify(utente);
return this.http.post('http://localhost:3000/utente', body)
.map((response: any) => {
console.log(response);
response.json();
})
.catch((error: Response) => Observable.throw(error.json()
));
}
}
The Moongose call:
var express = require('express');
var router = express.Router();
var User = require('../models/users');
router.post('/', function (req, res, next) {
var user = new User({
email: req.body.email,
password: req.body.password,
nome: req.body.nome,
cognome: req.body.cognome,
dataNascita: req.body.dataNascita,
telefono: req.body.telefono,
classifica: req.body.classifica
});
console.log(res);
user.save(function (err, result){
console.log(err);
console.log(res);
if (err){
return res.status(500).json({
titolo: "Errore durante il salvataggio",
errore: err
});
}
res.status(201).json({
messaggio: 'Utente salvato correttamente',
oggetto: res
});
});
});
I have 3 files with different schema. User has many notebooks and notebooks has many notes. Example of schemas :
UserSchema:
var mongoose = require('mongoose'),
Schema = mongoose.Schema;
var User = new Schema({
username: { type: String, require: true, index: { unique: true }, trim: true},
password: { type: String, require: true, select: true },
age: { type: Number, min: 0 },
firstname: String,
secondname: String,
token: String,
role: String,
city: String,
rememberMe: Boolean
});
module.exports = mongoose.model('User', User);
NotebookSchema:
var mongoose = require('mongoose'),
Schema = mongoose.Schema,
Note = require('./note'),
User = require('./user');
var NoteBook = new Schema({
creator: { type:Schema.ObjectId, ref:"User"},
name: String,
description: String
});
NoteBook.methods.getAllNotes = function(cb) {
Note.find({notebook: this}, function(err, noteList){
cb(noteList);
});
};
module.exports = mongoose.model('NoteBook', NoteBook);
NoteSchema:
var mongoose = require('mongoose'),
Schema = mongoose.Schema;
var NoteSchema = new Schema({
notebook: { type: Schema.Types.ObjectId, ref: 'NoteBook'},
name: String,
description: String,
content: String
});
module.exports = mongoose.model('Note', NoteSchema);
I get in request userId and I need to pack json all this line of data. User with Notebooks and Notebooks with note in one json. I triyed something like this:
function getTree(req, res) {
var data = [];
User.findOne({_id: req.body.userId}, function(err, user) {
NoteBook.find({creator: user._id}, function(err, notebookList) {
for (var idx in notebookList) {
Note.find({notebok: notebookList[idx]._id}, function(err, noteList) {
var children = [];
for (var noteIdx in noteList) {
children.push({
'text': noteList[idx].name,
'a_attr' : {
'data-node-type': 'note',
'data-node-id': noteList[idx]._id,
},
});
}
data.push({
'text': notebookList[idx].name,
'a_attr' : {
'data-node-type': 'notebook',
'data-node-id': notebookList[idx]._id,
},
'children': children
});
});
}
res.json({ tree: data });
});
});
}
but it doesn't work.
var async = require("async");
function getTree(req, res) {
var data = [];
User.findOne({_id: req.body.userId}, function(err, user) {
NoteBook.find({creator: user._id}, function(err, notebookList) {
async.forEach(notebookList, function(notebook, callback){
Note.find({notebok: notebook._id}, function(err, noteList) {
var children = [];
for (var noteIdx in noteList) {
children.push({
'text': noteList[idx].name,
'a_attr' : {
'data-node-type': 'note',
'data-node-id': noteList[idx]._id,
},
});
}
data.push({
'text': notebookList[idx].name,
'a_attr' : {
'data-node-type': 'notebook',
'data-node-id': notebook._id,
},
'children': children
});
});
}, function(err){
res.json({ tree: data });
});
});
});
}