node.js - err empty response when trying to insert into mongodb using node -
i'm quite new mean stack , trying create application has 2 data collections, 'users' , 'courses'. users have used login-and registration example , works fine. courses have tried re-create same structure (so files named including same course has similar 1 named user) courses having trouble creating new course , adding database. err empty response cannot understand why , how correct it. below (compressed) code, let me know if more information needed. have excluded client-side code since know error on server-side. server.js called following command client-side:
function create(course) { return $http.post('/api/courses', course).then(handlesuccess, handleerror); }
server.js
require('rootpath')(); var express = require('express'); var app = express(); var session = require('express-session'); var expressjwt = require('express-jwt'); var config = require('config.json'); var bodyparser = require('body-parser'); app.set('view engine', 'ejs'); app.set('views', __dirname + '/views'); app.use(bodyparser.urlencoded({ extended: false })); app.use(bodyparser.json()); app.use(session({ secret: config.secret, resave: false, saveuninitialized: true })); // use jwt auth secure api app.use('/api', expressjwt({ secret: config.secret }).unless({ path: ['/api/users/authenticate', '/api/users/register'] })); // routes app.use('/login', require('./controllers/login.controller')); app.use('/register', require('./controllers/register.controller')); app.use('/app', require('./controllers/app.controller')); app.use('/api/users', require('./controllers/api/users.controller')); app.use('/api/courses', require('./controllers/api/courses.controller.js')); //app.use('/registercourse', require('./controllers/create.course.controller')); // make '/app' default route app.get('/', function (req, res) { return res.redirect('/app'); }); var ipaddress = process.env.openshift_nodejs_ip || "127.0.0.1"; var port = process.env.openshift_nodejs_port || 3000; // start server var server = app.listen(port, ipaddress);
controllers/api/courses.controller
i don't think error in file since calls courseservice, wrong.
var config = require('config.json'); var express = require('express'); var router = express.router(); var courseservice = require('services/course.service'); router.post('/', createcourse); module.exports = router; function createcourse(req,res){ courseservice.create(req.body) .then(function(){ res.sendstatus(200); }) .catch(function(err) { res.status(400).send(err); }); }
services/course.services think error in file since not behave same services/user.services do. in services/user.services, db.users becomes bold , connected db.bind('users') not work in file, db.courses looks db.courses. have tried introducing namespace, seems connect db.courses db.bind('course') still same error.
var config = require('config.json'); var _ = require('lodash'); var jwt = require('jsonwebtoken'); var bcrypt = require('bcryptjs'); var q = require('q'); var mongo = require('mongoskin'); var connectionstring = "mongodb://localhost:27017/mean-stack-registration-login-example"; if(process.env.openshift_mongodb_db_url){ connectionstring = process.env.openshift_mongodb_db_url + "studycontrol"; } var db = mongo.db(connectionstring, { native_parser: true }); db.bind('courses'); var service = {}; service.create = create; module.exports = service; module.exports = service; function create(courseparam) { var deferred = q.defer(); //validation db.courses.findone( {coursename: courseparam.coursename}), function (err, course) { if (err) deferred.reject(err); if (course) { deferred.reject('coursename "' + courseparam.coursename + '" taken'); } else { createcourse() } }; function createcourse() { var course = courseparam db.courses.insert( course, function (err, doc) { if (err) deferred.reject(err); deferred.resolve(); }); } return deferred.promise; }
Comments
Post a Comment