javascript - Why isn't .save for Mongoose working in ReactJS? -
i have reactjs , redux connected mongodb, mongoose.
i have mongoose schema (user.js) set so:
var userschema = new schema({ email: { type: string, lowercase: true, unique: true, required: true }, })
and api controller receives email string request, , if nothing entered in text field, sends 422 error, , inside user.findone
, if email exists in database, throws 422 error , if not, user.save
save in database.
"use strict"; const user = require('../models/user') exports.register = function(req, res, next) { const email = req.body.email; console.log('error 1') if(!email) { return res.status(422).send({ error: 'you must enter email address.'}) console.log('error 1') } user.findone({ email: email }, function(err, existinguser) { if(err) { return next(err); } console.log('error 2') if(existinguser) { return res.status(422).send({ error: 'that email address in use.'}) } console.log('error 3') let user = new user({ email: email, }) console.log('error 4') user.save(function(err, user) { if(err) { return next(err); } console.log('error 5') res.status(201).json({ user: user, }) }) }) console.log('error 6') }
and making post request such:
export function registeruser({ email }) { return function(dispatch) { axios.post(`${api_url}/auth/register`, { email }) .then(response => { console.log('this testing purpose') console.log(response) dispatch({ type: auth_user }); }) .catch((error) => { errorhandler(dispatch, error.response, auth_error) }); } }
i made several post requests , successful status api sever config: {'database': 'mongodb://localhost/practicedb', 'port': process.env.port || 3000}
, yet data never gets saved , database (practicedb) doesn't show on terminal.
everything seem set correctly why problem? missing something? insight or guidance appreciated.
thank in advance!
here logs , what's options request never made:
correct if i'm wrong bare save method not async. save method return promise. see http://mongoosejs.com/docs/promises.html
edit
user.save().then(function(doc) { if (!doc) { next(new error('error while persisting!')); } console.log('error 5'); res.status(201).json({ user: doc }); });
you can achieve promised library (q, bluebird) or use es6 promise. alternatively use async.
example q. not tested:
"use strict"; const user = require('../models/user'); const q = require('q'); //add https://github.com/kriskowal/q exports.register = function(req, res, next) { const email = req.body.email; console.log('error 1') if(!email) { return res.status(422).send({ error: 'you must enter email address.'}) console.log('error 1') } var deferred = q.defer(); user.findone({ email: email }, function(err, existinguser) { if(err) { return next(err); } console.log('error 2') if(existinguser) { return res.status(422).send({ error: 'that email address in use.'}) } console.log('error 3') let user = new user({ email: email, }) console.log('error 4') user.save(function(err, user) { if(err) { deferred.reject(err); return next(err); } console.log('error 5') deferred.resolve(user); // }); res.status(201).json({ user: deferred.promise, }) }) console.log('error 6') }
Comments
Post a Comment