如何使用Mean堆栈将数据发布到mongo集合? [英] How to post data to mongo collection using Mean stack?

查看:80
本文介绍了如何使用Mean堆栈将数据发布到mongo集合?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个对象,其中包含客户端工厂中打印的客户端的名字和姓氏,所以我是后端开发的新手,所以我创建了用户架构和api来发布数据,但是它引发了404错误。下面提到错误。知道什么是错误的吗?

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 . 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);

        }

服务器代码

app> routes.js

app > routes.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'')); 
      });

  };

app下的文件夹> api> user

Folders under app > api > user

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));
}

错误

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"}


推荐答案

角度错误堆栈跟踪很清楚。

你没有在您的快速路由器中处理POST查询的特定端点。

Angular error stack trace is very clear.
You do not handle this specific endpoint for your POST query in your express router.

因此服务器会抛出 404错误

尝试这样的事情:

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

使用express(和NodeJS),您必须明确指定对服务器资源的每次访问。

With express (and NodeJS) you must explicitly specify each access to server resources.

作为旁注,你非常接近这一点,但试着保持你的应用程序逻辑简单以保持可维护性。

As side note, you are pretty close to this but try to keep your application logic simple for maintenability.

这篇关于如何使用Mean堆栈将数据发布到mongo集合?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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