发布到mongodb后得到回复

时间:2015-01-31 01:51:47

标签: javascript node.js mongodb

我尝试使用x-www-formurlencoded通过邮递员测试我的代码写入mongodb,但我一直收到这个错误:

<!DOCTYPE html>
<html>
    <head>
        <title></title>
        <link rel="stylesheet" href="/stylesheets/style.css">
        </head>
        <body>
            <h1>Not Found</h1>
            <h2>404</h2>
            <pre>Error: Not Found
    at Layer.app.use.res.render.message [as handle] (c:\Users\Tomix\Documents\Source\Tomix\app.js:39:15)
    at trim_prefix (c:\Users\Tomix\Documents\Source\Tomix\node_modules\express\lib\router\index.js:226:17)
    at c (c:\Users\Tomix\Documents\Source\Tomix\node_modules\express\lib\router\index.js:198:9)
    at Function.proto.process_params (c:\Users\Tomix\Documents\Source\Tomix\node_modules\express\lib\router\index.js:251:12)
    at next (c:\Users\Tomix\Documents\Source\Tomix\node_modules\express\lib\router\index.js:189:19)
    at next (c:\Users\Tomix\Documents\Source\Tomix\node_modules\express\lib\router\index.js:166:38)
    at next (c:\Users\Tomix\Documents\Source\Tomix\node_modules\express\lib\router\index.js:150:14)
    at next (c:\Users\Tomix\Documents\Source\Tomix\node_modules\express\lib\router\index.js:166:38)
    at Function.proto.handle (c:\Users\Tomix\Documents\Source\Tomix\node_modules\express\lib\router\index.js:234:5)
    at Layer.router (c:\Users\Tomix\Documents\Source\Tomix\node_modules\express\lib\router\index.js:23:12)</pre>
        </body>
        <!--Live reload script -->
        <script type="text/javascript" src="http://localhost:35729/livereload.js"></script>
    </html>
所以我要做的是创建一个具有平均堆栈的电子商务网站,所以我试图构建应用程序文件和文件夹以反映服务器端和客户端。 这里的代码是将数据保存到数据库(mongodb),但是从我能够推断出的错误消息是,它应该以json格式返回响应,它会抛出错误消息。
exports.create = function(req, res) {
  console.log('I got called', req.body);

  var product = new Product(req.body);

  product.save(function(err) {
    if (err) {

      return res.send(400, {
        message: err
      });
    } else {

      res.json(product);
    }
  });
};
我的app.js文件

'use strict';

var express = require('express');
var path = require('path');
var favicon = require('static-favicon');
var logger = require('morgan');
var cookieParser = require('cookie-parser');
var bodyParser = require('body-parser');
var methodOverride = require('method-override');

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

/*connecting the to database*/
require('./config/db');

var app = express();

// view engine setup
app.set('views', path.join(__dirname, 'app/views'));
app.set('view engine', 'jade');

app.use(favicon());
app.use(logger('dev'));
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: true }));
app.use(methodOverride('X-HTTP-Method-Override'));
app.use(cookieParser());



app.use(express.static(path.join(__dirname, 'public')));

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

/// catch 404 and forwarding to error handler
app.use(function(req, res, next) {
    var err = new Error('Not Found');
    err.status = 404;
    next(err);
});

/// error handlers

// development error handler
// will print stacktrace
if (app.get('env') === 'development') {
    app.use(function(err, req, res, next) {
        res.status(err.status || 500);
        res.render('error', {
            message: err.message,
            error: err
        });
        next();
    });
}

// production error handler
// no stacktraces leaked to user
app.use(function(err, req, res, next) {
    res.status(err.status || 500);
    res.render('error', {
        message: err.message,
        error: {}
    });
    next();
});

//Routes
require('./app/routes/product.server.routes.js')(app);

exports = module.exports = app;

请在线搜索可能的解决方案,我得到的一些建议是我需要在我的快速配置中添加方法覆盖。

1 个答案:

答案 0 :(得分:0)

我认为你错过了等式的一部分:

发布/信息 - &gt;连接器/驱动器 - &gt; mongodb的/数据库

哟不能通过Postma将JSON发布到mongo。你需要找到一个mongodb解决方案,用于javascript来玩它。例如,你可以尝试使用Node.js。在这里,我列出了连接到mongo的所有语言可能性。

http://docs.mongodb.org/ecosystem/drivers/

希望它有所帮助!