MongoDB document not saved but showing success - mongodb

I have a problem. I'm trying to save answers from a survey. My problem is that I'm sending these answers through axios and they are showing in the console.log from the server, which means that my data is reaching the server safely. But data is not being saved. Also no error is shown.
Server.js
router.route('/answers')
//post answers to database
.put(function(req, res){
var survey = new Survey();
survey.name = req.body.name;
survey.email = req.body.email;
survey.q_id = req.body.q_id;
survey.q_text = req.body.q_text;
survey.answer = req.body.answer;
console.log(Survey.collection)
survey.save(function(err){
if(err){
console.log(err)
return ;
}
res.send(survey);
});
})
Survey.js (Model)
var mongoose = require('mongoose');
var Schema = mongoose.Schema;
var ObjectId = mongoose.Schema.Types.ObjectId;
var question = new Schema({
q_text:String,
order: Number,
options:[{
type: {type: String},
value: {type: String}
}]
});
module.exports = mongoose.model('Question',question);
Code that is posting the data
const url = 'http://localhost:3100/api/answers';
axios.put(url, survey)
.then(res => {
console.log('Successfully posted');
console.log(res);
setSubmitting(false);
setStatus({submitted: true});
})
.catch(err => {
console.log(err);
})

Try this :
survey.save(function(err, savedSurvey){
if(err){
console.log(err)
return ;
}
res.send(savedSurvey);
});

Related

Can't save to mongoDB's database

While sending a post request i written the following code :
var email = req.body.email ;
var newDetails = { email: email };
Details.create(newDetails);
console.log(newDetails);
while sending the request. The console.log shows me the correct details,
However in the mongo shell the only collection that exist is "details" and it's empty .
That's the Mongoose Schema:
var mongoose = require("mongoose");
var DetailsSchema = mongoose.Schema({
email: String
});
module.exports = mongoose.model("Details", DetailsSchema);
I'm using NodeJS.
Thanks in advance.
Your Mongoose Model should be like
const mongoose = require("mongoose");
const Scheme = mongoose.Schema;
const DetailsSchema = new Scheme({
email: String
});
module.exports = mongoose.model("Details", DetailsSchema);
Node js Code should be like
var detailsModel = require('../model/Details.js');//path of mongoose model
var detailsData = new detailsModel();
detailsData.email = req.body.email;
detailsData.save(function (err, savedJob) {
if (err) {
return res.send(err);
} else {
return res.send(savedJob);
}
});
To save data in mongoDB's Database
You can use this way
var detailsData = new detailsModel();
detailsData.save()
.then(business => {
res.status(200).json({'Details': 'newDetails added successfully'});
})
.catch(err => {
res.status(400).send("unable to save to database");
});
With this, you can also handle error easily.

Mongoose - How to query within an hook?

I'm not 100% sure about how to formulate this question.
Consider the following code of the 'message.js' model file.
I have a "post remove" hook in which I query the User collection, which is defined in the user.js file (That file is pretty much the same as this one).
Now, what I'd like to do is to query the Message collection(which is defined in this file) inside the "pre save" hook.
Is it possible to do that ?
var mongoose = require('mongoose');
var Schema = mongoose.Schema;
var User = require('./user');
var schema = new Schema({
content: {type: String, required: true},
user: {type: Schema.Types.ObjectId, ref: 'User'}
});
schema.post('remove', function (message) {
User.findById(message.user, function (err, user) {
user.messages.pull(message);
user.save();
});
});
schema.pre('save', function(next) {
//Here I want to query the Message collection
next();
});
module.exports = mongoose.model('Message', schema);
schema.pre('save', function(next) {
let MessageModel = mongoose.model('Message');
MessageModel.find(findQuery, (err, results) => {
if(err) handleError();
//Do stuff with results
next();
});
});

check if username and email is unique in database

Tried searching the net for 2 days and still could not find a specific answer. I have the below node.js code for user routes and models. How can I check if the username and email has never appear in the MongoDB, and prompt the user a message if there is?
model:
var mongoose = require('mongoose');
var bcrypt = require('bcryptjs');
// User Schema
var UserSchema = mongoose.Schema({
username:{type: String , required:true, index: true, unique:true},
email:{type: String, required:true, index: true, unique:true},
password:{type: String, required:true}
});
module.exports = mongoose.model('User', UserSchema)
route:
var express = require('express');
var router = express.Router();
var User = require('../models/user');
var bcrypt = require('bcryptjs');
// Get Homepage
router.get('/', function(req, res){
res.render('index');
});
router.get('/register',function(req,res){
res.render('register');
});
// submit form
router.post('/submit', function(req, res){
// retrieve data from posted HTML form
var username = req.body.username;
var email = req.body.email;
var password = req.body.password;
var password_confirm = req.body.password_confirm;
// express validator
req.checkBody('username','Username is required').notEmpty();
req.checkBody('email','Email is required').notEmpty();
req.checkBody('email','Invalid email').isEmail();
req.checkBody('password','Password is required').notEmpty();
req.checkBody('password','Password must contain at least 6 characters').isLength({min:6});
req.checkBody('password_confirm','Password mismatch').equals(req.body.password);
// store the errors
var errors = req.validationErrors();
if(errors){res.render('register',{errors:errors});}
else {
// hash password
var salt = bcrypt.genSaltSync(10);
var hash = bcrypt.hashSync(password, salt);
password=hash;
// load data into model
var newUser = new User ({username:username, email:email, password:password});
// save the new user
newUser.save(function(err,newUser){
if(err){console.error(err);}
// console.error is same, but in stderr form
else{
console.log('new user saved successfully');
console.log(newUser);
}
});
res.redirect('/');
}
});
module.exports = router;
app.post('/authenticate', function(req, res) {
var user = new User({
username: req.body.username
});
user.save(function(err) {
if (err) {
if (err.name === 'MongoError' && err.code === 11000) {
// Duplicate username
return res.status(500).send({ succes: false, message: 'User already exist!' });
}
// Some other error
return res.status(500).send(err);
}
res.json({
success: true
});
});
})
You have to catch the error and return it to the front end of your application. This code above should demonstrate how to achieve this by using server status 500. Regarding searching the web, this answer and question are quite similar to this previous question:
How to catch the error when inserting a MongoDB document which
violates an unique index?
I hope this helped to some extend.

Can't set headers after they are sent. Error when Creating a sub-document with Mongo

I am trying to create a sub-document with my express API through postman with the following request:
POST /api/course/58c6f76e06e6edda1b000007/subject/58c6f85280a5d6591c000007/question
I am also sending x-www-forum-urlencoded data with names question and answer.
Here is the error that is output in the terminal:
Error: Can't set headers after they are sent.
at ServerResponse.OutgoingMessage.setHeader (_http_outgoing.js:335:11)
at ServerResponse.header (/home/tyler/Dropbox/Projects/Curricula/node_modules/express/lib/response.js:719:10)
at ServerResponse.send (/home/tyler/Dropbox/Projects/Curricula/node_modules/express/lib/response.js:164:12)
at ServerResponse.json (/home/tyler/Dropbox/Projects/Curricula/node_modules/express/lib/response.js:250:15)
at sendJSONResponse (/home/tyler/Dropbox/Projects/Curricula/API/controllers/question.js:8:8)
at Promise.<anonymous> (/home/tyler/Dropbox/Projects/Curricula/API/controllers/question.js:36:10)
at Promise.<anonymous> (/home/tyler/Dropbox/Projects/Curricula/node_modules/mpromise/lib/promise.js:171:8)
at emitOne (events.js:77:13)
at Promise.emit (events.js:169:7)
at Promise.emit (/home/tyler/Dropbox/Projects/Curricula/node_modules/mpromise/lib/promise.js:88:38)
Here is my controller file and relevant data models:
var mongoose = require('mongoose');
var Course = mongoose.model('Course');
var Subject = mongoose.model('Subject');
var Question = mongoose.model('Question');
var sendJSONResponse = function(res, status, content){
res.status(status);
res.json({content});
};
var addQuestion = function(req, res, subject){
if(!subject){
sendJSONResponse(res, 404, {"message":"subjectid not found"});
return;
}
else{
subject.questions.push({question: req.body.question,
answer: req.body.answer,
falseAnswer: req.body.falseanswer});
subject.save(function(err, course){
var thisQuestion
if(err){
sendJSONResponse(res, 400, err);
} else{
thisQuestion = subject.questions[subject.questions.length - 1];
sendJSONResponse(res, 201, thisQuestion);
}
});
}
}
var seekSubject = function(req, res, course){
if(req.params && req.params.subjectid){
Subject.findById(req.params.subjectid).exec(function(err, subject){
if(!subject){
sendJSONResponse(res, 404, {"message":"subjectid not found"});
}
else if(err){
sendJSONResponse(res, 404, err);
return;
}
sendJSONResponse(res, 200, subject);
return subject
});
} else{
sendJSONResponse(res, 404, {
"message":"no subjectid in request"
});
}
};
module.exports.makeQuestion = function(req, res){
var courseid = req.params.courseid;
if(courseid) Course.findById(courseid).select('subjects').exec(
function(err, course){
if(err){
sendJSONResponse(res, 400, err);
return;
}
else var subject = seekSubject(req, res, course);
addQuestion(req, res, subject);
});
}
Course Model
var mongoose = require('mongoose')
var subject = require('./subject.js');
var courseSchema = new mongoose.Schema({
name : {type: String,
unique: true,
required: true},
subjects : [subject.schema]
});
Subject Model
module.exports = mongoose.model('Course', courseSchema);
var mongoose = require('mongoose')
var question = require('./question.js');
var subjectSchema = new mongoose.Schema({
name : String,
questions : [question.schema]
});
module.exports = mongoose.model('Subject', subjectSchema);
Question model
var mongoose = require('mongoose')
var questionSchema = new mongoose.Schema({
question: {type: String, required: true},
answer: {type: String, required: true},
falseAnswer: [String]
});
module.exports = mongoose.model('Question', questionSchema);
What am I doing wrong in creating the entry?
It could be in this area
else var subject = seekSubject(req, res, course);
addQuestion(req, res, subject);
those 2 function, seekSubject and addQuestion seem like that are both calling sendJSONResponse and they both seem like that they sending a response
res.status(status); res.json({content});
So if both functions are being called in that area you are likely sending 2 responses and that is why you are getting that error.
You are doing good by separating the concerns but you got to get the error stuff separate too. your telling the server to respond twice when you call the 2 function. I think your calling both functions. I haven't ran your code.
You get this because you are sending 2 responses when you should send just 1. Try doing the following:
res.status(500).json({ error: 'message' });
Or you might want to try using return before sendJSONResponse(res, status, content); like so for example:
return sendJSONResponse(res, 404, {"message":"subjectid not found"});
Hope this helps you out.

Can't get data from mongodb

I am trying to access a document and get the results to console. Mongoose debug shows that there is a document, yet returns an empty record.
//mongoose connect to db
var db = mongoose.createConnection('mongodb://localhost/mean-dev1/', function(err) {
if (err) throw err;
});
db.once('open', function callback() {
var tripSchema = mongoose.Schema(
{ title: String,
fromDate: Date,
tillDate: Date,
price: Number,
details: String,
availability: Number
});
// Model definition
var TripObj = db.model('TripObj', tripSchema, 'tripmodel');
TripObj.find(function(err, data) {
if (!err){
console.log('%s', data); //gets undefined
console.log("written...", data);
}else{
throw err;
process.exit();
}
});
}
and here is the console output
...
Migrations: Up to date at version 003
Mongoose: tripmodel.find({}) { fields: { title: 1 } }
is undefined?
written... []
Figured it out, the problem was with my initial connection. So changed the code a bit.
//connects to mongoose datasource and populates the data.json file in app
mongoose.disconnect();
mongoose.connect('mongodb://localhost/tripDB/', function(err) {
if (err) throw err;
}),
Schema = mongoose.Schema
var db = mongoose.connection;
db.once('open', function callback() {
var tripSchema = mongoose.Schema(
{
availability: Number,
details: String,
fromDate: Date,
price: Number,
tillDate: Date,
title: {type:String, ref:'TripObj'}
});
// Model definition
var TripObj = mongoose.model('TripObj', tripSchema, 'trips');
TripObj.find(function(err, data) {
if (!err){
var newdata = data;
//console.log('%s is undefined?', newdata);
fs.writeFileSync(path.join(__dirname, '../../../../' + 'content/themes/traveller/assets/js' + '/data.json'), JSON.stringify(newdata));
console.log("written succesfully", data);
process.exit();
}else{
throw err;
}
});
});