简体   繁体   English

如何使用均值堆栈将数据发布到mongo集合?

[英]How to post data to mongo collection using Mean stack?

I have a object that contains firstname and lastname from client it is printing in client factory so i am new to backend development so i created user schema and api to post the data but it is throwing 404 error . 我有一个包含来自客户端的名字和姓氏的对象,该对象正在客户端工厂中打印,因此我是后端开发的新手,因此我创建了用户架构和api来发布数据,但抛出404错误。 Error mentioned below in question. 下面提到的错误。 Any idea what is implemented wrong ? 知道实施错误了吗?

clientFactory.js clientFactory.js

 create : function(userData) {
            console.log('factory',userData);
            return $http.post('/api/users', userData);

        }

Server code 服务器代码

app > routes.js 应用> route.js

  module.exports = function(app) {

      app.use('api/users', require('./api/user'));
      app.get('*', function(req, res) {
          res.sendfile('./public/views/index.html'); // load our public/index.html file
          // res.sendFile(path.join(__dirname, ''../public/views/index.html'')); 
      });

  };

Folders under app > api > user 应用> api>用户下的文件夹

user.index.js user.index.js

var express = require('express');
var controller = require('./user.controller');

var router = express.Router();

router.get('/', controller.index);
router.post('/',controller.create);

module.exports = router;

user.model.js user.model.js

var mongoose = require('bluebird').promisifyAll(require('mongoose'));

var UserSchema = new mongoose.Schema({
  firstName: String,
  lastName: String
});

module.exports = mongoose.model('User', UserSchema);

user.controller.js user.controller.js

exports.create = function(req, res) {
  console.log(req.body);
  User.createAsync(req.body)
    .then(responseWithResult(res, 201))
    .catch(handleError(res));
}

Error 错误

angular.js:12410 POST http://localhost:8080/api/users 404 (Not Found)
(anonymous) @ angular.js:12410
p @ angular.js:12155
(anonymous) @ angular.js:11908
(anonymous) @ angular.js:16648
$eval @ angular.js:17972
$digest @ angular.js:17786
$apply @ angular.js:18080
(anonymous) @ angular.js:26749
cg @ angular.js:3613
d @ angular.js:3601
angular.js:14328 Possibly unhandled rejection: {"data":"Cannot POST /api/users\n","status":404,"config":{"method":"POST","transformRequest":[null],"transformResponse":[null],"jsonpCallbackParam":"callback","url":"/api/users","data":{"firstName":"Kun","lastName":"Zhang"},"headers":{"Accept":"application/json, text/plain, */*","Content-Type":"application/json;charset=utf-8"}},"statusText":"Not Found"}

Angular error stack trace is very clear. 角度错误堆栈跟踪非常清晰。
You do not handle this specific endpoint for your POST query in your express router. 您在快速路由器中不为POST查询处理此特定端点。

So the server throw a 404 error , as it should. 因此,服务器应该抛出404错误

Try something like this: 尝试这样的事情:

router
    .post('/api/users', function (req, res) {
       // do what you want with your user here
    })

With express (and NodeJS) you must explicitly specify each access to server resources. 使用express(和NodeJS)时,必须显式指定对服务器资源的每次访问。

As side note, you are pretty close to this but try to keep your application logic simple for maintenability. 附带说明一下,您已经很接近了这一点,但是请尝试使应用程序逻辑简单易维护。

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

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