Inserts NULL - Node Js // MongoDB

Deadly 提交于 2020-01-15 09:36:47

问题


  • I don´t know why this code inserts NULL.

  • When the post request is sent I receive (from res.json(req.body) in index.js) an empty "{}"

  • I´m using NodeJs, Express 4 and MongoDB.

  • The name attributes in the post are the same than in the jade template. I have two forms in the same .jade but with differents names, obviously.

  • This is not all code, I just put the most important and what is related to the question

App.js

var express = require('express');
var bodyParser = require('body-parser');

var mongo = require('mongodb');
var monk = require('monk');
var db = monk('localhost:27017/registredUsers');

var routes = require('./routes/index');
var users = require('./routes/users');

var app = express();

app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: true }));

app.use('/', routes);
app.use('/users', users);

module.exports = app;

index.js

var express = require('express');
var bodyParser = require('body-parser');

router.post('/adduser', function(req, res) {

    var db = req.db;

    var userName = req.body.username;
    var userEmail = req.body.useremail;
    var userPassword = req.body.userpassword;

    var collection = db.get('usercollection');

    collection.insert({
        username : userName,
        email : userEmail,
        password : userPassword
    }, function (err, doc) {
        if (err) {
            res.send("There was a problem adding the information to the database.");
        }
        else {
            res.json(req.body);
        }
    });
});

module.exports = router;

回答1:


Have you tried putting

var mongo = require('mongodb');
var monk = require('monk');
var db = monk('localhost:27017/registredUsers');

At the top of your index.js and removing var db = req.db from the route.

Also, you can return the newly created document from the call back in your response.

Your index.js file would now look like this.

index.js

var express = require('express');
var bodyParser = require('body-parser');
var mongo = require('mongodb');
var monk = require('monk');
var db = monk('localhost:27017/registredUsers');

router.post('/adduser', function(req, res) {

    var userName = req.body.username;
    var userEmail = req.body.useremail;
    var userPassword = req.body.userpassword;

    var collection = db.get('usercollection');

    collection.insert({
        username : userName,
        email : userEmail,
        password : userPassword
    }, function (err, doc) {
        if (err) {
            res.send("There was a problem adding the information to the database.");
        }
        else {
            res.json(req.body); // <- here you could return the new object instead.
            // res.json(doc);
        }
    });
});

module.exports = router;

edit

To assist the OP I made the following suggestion:

Hardcode var userName = 'test' and return the created document in res.json(doc) in order to test that the database is updating.



来源:https://stackoverflow.com/questions/39057898/inserts-null-node-js-mongodb

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!