插入NULL-Node Js//MongoDB [英] Inserts NULL - Node Js // MongoDB

查看:55
本文介绍了插入NULL-Node Js//MongoDB的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

  • 我不知道为什么这段代码会插入NULL.

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

发送请求后,我从index.js中的res.json(req.body)接收到一个空的"{}"

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

我正在使用NodeJ,Express 4和MongoDB.

I´m using NodeJs, Express 4 and MongoDB.

帖子中的名称属性与玉器中的名称属性相同模板.显然,我在同一个.jade中有两种形式,但名称不同.

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.

这还不是全部代码,我只是将最重要的内容以及与该问题有关的内容

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;

推荐答案

您是否尝试过

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

在index.js的顶部,并从路由中删除 var db = req.db .

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.

您的 index.js 文件现在看起来像这样.

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;

修改

为协助OP,我提出了以下建议:

To assist the OP I made the following suggestion:

硬编码 var userName ='test',然后在 res.json(doc)中返回创建的文档,以测试数据库是否正在更新.

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

这篇关于插入NULL-Node Js//MongoDB的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆