简体   繁体   English

发布到mongodb后获得响应

[英]Getting response after posting to mongodb

I tried testing my code written to post data to mongodb via postman using x-www-formurlencoded but I keep getting this error: 我尝试使用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> 
So what am trying to do is to create an e-commerce website with mean stack,so am trying to structure the app files and folder to reflect the server side and the client side. 因此,要做的是创建一个具有均值堆栈的电子商务网站,并尝试构建应用程序文件和文件夹以反映服务器端和客户端。 The code here is to save data to database (mongodb), but from the error message I was able to deduce is that at the point it ought to return response in json format it throws the error message. 此处的代码用于将数据保存到数据库(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); } }); }; 
My app.js file 我的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; 

Please am stuck have search online for possible solution, some of the suggestion I got was I need to add method-override to my express configuration. 请卡在网上搜索可能的解决方案,我得到的一些建议是我需要在我的快速配置中添加方法重写。

I think that you missed a part of the equation: 我认为您错过了方程式的一部分:

Post/Info -> Connector/Driver -> Mongodb/Database 帖子/信息->连接器/驱动程序-> Mongodb /数据库

Yo can not post JSON to mongo via Postma. 您无法通过Postma将JSON发布到mongo。 You'll need to find a mongodb solution for javascript to play with it. 您需要找到用于JavaScript的mongodb解决方案才能使用它。 You can try it with Node.js for example. 您可以使用例如Node.js进行尝试。 Here I give you a list of all the language possibilities to connect to mongo. 在这里,我为您列出了连接到mongo的所有语言。

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

Hope it helps! 希望能帮助到你!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM