【问题标题】:Using MEAN to store data使用 MEAN 存储数据
【发布时间】:2017-04-28 07:01:33
【问题描述】:

我在学校项目方面需要帮助。我正在制作一个应用程序来创建和更新需要解决的问题列表。

我有一个输入字段,我应该在其中插入问题,然后它应该生成一个 ID、一个“打开”状态和一个日期戳。

然后问题将显示在我的输出表中。我已经解决了我可以显示数据库中的内容。现在我需要解决我存储新数据的部分。

我的计划是在我的按钮 AddIssue 上使用 ng-click 并调用一个也名为 AddIssue 的函数。然后使用 $http post 发送数据槽,然后使用 moongose 存储数据

HTML

 <div ng-controller="inputCtrl">
 <p>New issue <input class="form-control" type="text" ng-model="newIssue"></p>
 <button ng-click="AddIssue()"> Add </button>
 </div>

<div ng-controller="tableCtrl">
<table class="table table-striped">
<thead>
  <tr>
    <th>Status</th>
    <th>Issue</th>
    <th>Date</th>
    <th>ID</th>
  </tr>
</thead>
<tbody>
  <tr ng-repeat="issue in issueList">
        <td>{{issue.status}}</td>
        <td>{{issue.issue}}</td>
        <td>{{issue.date}}</td>
        <td>{{issue.id}}</td>
  </tr>
</tbody>
</table>
</div>

我的 JS 文件 controller.js

  var myApp = angular.module('myApp', []);

 // Controller for input
 myApp.controller('inputCtrl', function($scope) {

// Call function when click on AddIssue btn
$scope.AddIssue = function() {

    console.log("Click click....")

    // Use Post to send data to database    
    $http.post('/issueList').sucess( function(response){
       $scope.newIssue = 'New issue';
    })


 }

}); // End of input

server.js

 /* Connect to db issues */
 mongoose.Promise = global.Promise;
 mongoose.connect('mongodb://localhost/issues');

 var db = mongoose.connection;
 db.on('error', console.error.bind(console, 'connection error:'));

/* Connect to MongoDB */
db.once('open', function (callback) {
console.log("Connected to db");

// Created b-schema
issueSchema = mongoose.Schema({
    id: String,
    issue: String,
    date: String,
    status: String
});

// Create Model
Issue = mongoose.model('Issue', issueSchema );

到目前为止,我已经来了,现在我需要帮助,首先如何将数据从 controller.js 发送到 server.js

第二次使用 mongoose 将其存储在我的 mongodb 问题上

【问题讨论】:

  • 我认为你应该看一些例子来帮助你。这是一篇关于使用 MEAN 堆栈创建待办事项应用程序的好文章:scotch.io/tutorials/…

标签: angularjs mongodb express mongoose mean


【解决方案1】:

您需要在 server.js 中为您的 POST 定义一个路由,在其中您可以将问题对象保存在 mongodb 中。

类似:

//addIssue
    app.post('/addIssue', function(request, response) {
        var issue = new Issue({
            id: request.body.id,
            issue: request.body.issue,
            date: request.body.date,
            status: request.body.status
        });

        return issue.save(function(err) {
           if(!err) {
               return response.send(issue);
           } else {
               console.log("ERROR adding issue:" + err);
           }
        });
    });

你还需要安装一些 npm 包。例如:

  • body-parser (npm install body-parser)
  • 快递(npm 安装快递)

等等

您还需要将它们导入到您的 server.js 文件中:

var express =  require('express');
var bodyParser = require ('body-parser');

这只是一个阐明这个想法的例子。在生产环境中,情况略有不同。

看看: https://scotch.io/tutorials/setting-up-a-mean-stack-single-page-application

【讨论】:

  • 谢谢!我会尝试这个解决方案并按照这个想法工作
  • 你知道为什么我得到 issueSchema 不是一个函数吗?
  • 这是“新问题(...”而不是“新问题架构(...”。
  • 是的,我找到了答案 =) 现在我有了这个对象,但我的下一个问题是我得到 Issue.save 不是一个功能
  • 把代码粘贴到 pastebin.com 上,如果你愿意,可以把链接发给我。
猜你喜欢
  • 2016-03-26
  • 1970-01-01
  • 2016-07-09
  • 2012-12-02
  • 2012-01-25
  • 2023-04-02
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多