<bdo id='InCiz'></bdo><ul id='InCiz'></ul>

      <small id='InCiz'></small><noframes id='InCiz'>

      <legend id='InCiz'><style id='InCiz'><dir id='InCiz'><q id='InCiz'></q></dir></style></legend>
      1. <i id='InCiz'><tr id='InCiz'><dt id='InCiz'><q id='InCiz'><span id='InCiz'><b id='InCiz'><form id='InCiz'><ins id='InCiz'></ins><ul id='InCiz'></ul><sub id='InCiz'></sub></form><legend id='InCiz'></legend><bdo id='InCiz'><pre id='InCiz'><center id='InCiz'></center></pre></bdo></b><th id='InCiz'></th></span></q></dt></tr></i><div id='InCiz'><tfoot id='InCiz'></tfoot><dl id='InCiz'><fieldset id='InCiz'></fieldset></dl></div>

        <tfoot id='InCiz'></tfoot>

        如何使用 MEAN 堆栈(MySQL、Express、Angular、Node)将数

        时间:2023-06-06
          <bdo id='823tK'></bdo><ul id='823tK'></ul>
          <tfoot id='823tK'></tfoot>

                <i id='823tK'><tr id='823tK'><dt id='823tK'><q id='823tK'><span id='823tK'><b id='823tK'><form id='823tK'><ins id='823tK'></ins><ul id='823tK'></ul><sub id='823tK'></sub></form><legend id='823tK'></legend><bdo id='823tK'><pre id='823tK'><center id='823tK'></center></pre></bdo></b><th id='823tK'></th></span></q></dt></tr></i><div id='823tK'><tfoot id='823tK'></tfoot><dl id='823tK'><fieldset id='823tK'></fieldset></dl></div>

                <small id='823tK'></small><noframes id='823tK'>

              • <legend id='823tK'><style id='823tK'><dir id='823tK'><q id='823tK'></q></dir></style></legend>
                  <tbody id='823tK'></tbody>

                1. 本文介绍了如何使用 MEAN 堆栈(MySQL、Express、Angular、Node)将数据插入数据库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                  问题描述

                  限时送ChatGPT账号..

                  如何使用 MEAN 堆栈(MySQL、Express、Angular、Node)将数据插入数据库?我在插入时遇到了问题.请帮我解决这个问题.这是我的代码.我是 MEAN 的新手.

                  index.html

                  <头><link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css"><标题>注册页面</title><身体><div class="container" ng-app="bookitnow" ng-controller="myCtrl"><h1>注册</h1><form method="post"><div class="form-group"><input type="text" class="form-control" id="firstName" ng-model="fname" placeholder="First Name" required>

                  <div class="form-group"><input type="text" class="form-control" id="lastName" ng-model="lname" placeholder="Last Name" required>

                  <div class="form-group"><input type="email" class="form-control" id="email" ng-model="email" placeholder="Email Address" required>

                  <div class="form-group"><input type="text" class="form-control" id="phone" ng-model="phone" placeholder="Phone" required>

                  <button type="submit" class="btn btn-info" ng-click="submit()">提交</button></表单>

                  <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.5.8/angular.min.js"></script><script src="controllers/controller.js"></script></html>

                  server.js

                  var express = require("express");var app = express();var bodyParser = require('body-parser');var mysql = require('mysql');var connection = mysql.createConnection({主机:'本地主机',用户:'根',密码 : '',数据库:'bookitnow'});connection.connect(function(err) {如果(!错误)console.log('数据库已连接');别的console.log('数据库连接错误.');});app.use(express.static(__dirname + '/public'));app.get('/index.html', function(req, res){res.sendFile(__dirname + '/public' + 'index.html');});app.post('/index.html', function(req, res, next) {var data = req.body;console.log('收到请求:', 数据);connection.query('INSERT INTO register SET ?', data, function(err,res){如果(错误)抛出错误;console.log('记录插入');});app.listen(5500);console.log('你好,来自 server.js');

                  controller.js

                  var app = angular.module('bookitnow',[]);app.controller('myCtrl', function($scope,$http){$scope.submit = function() {变量数据 = {fName : $scope.fname,lName : $scope.lname,电子邮件:$scope.email,电话:$scope.phone}};$http.post('/index.html', &scope.data).success(功能(数据,状态,标题,配置){$scope.result = 数据;console.log("插入成功")}).错误(功能(数据,状态,标题,配置){$scope.result = "数据:" + 状态;});

                  解决方案

                  在您的 controller.js 中,尝试以下操作,看看会发生什么.

                  var app = angular.module('bookitnow',[]);app.controller('myCtrl', function($scope, $http){$scope.submit = function() {变量数据 = {fName : $scope.fname,lName : $scope.lname,电子邮件:$scope.email,电话:$scope.phone};$http.post('/index.html', 数据).then(功能(响应){控制台日志(响应)},功能(响应){控制台日志(响应)});};});

                  How to insert data into database using MEAN stack (MySQL, Express, Angular, Node)? I am facing the problem at the time of insertion. Please help me to solve this. Here is my code. I'm newbie in MEAN.

                  index.html

                  <html ng-app="bookitnow">
                  <head>
                      <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
                  
                      <title> Registration Page </title>
                  </head>
                  <body>
                  <div class="container" ng-app="bookitnow" ng-controller="myCtrl">
                    <h1>Register</h1>
                      <form method="post">
                      <div class="form-group">
                          <input type="text" class="form-control" id="firstName" ng-model="fname" placeholder="First Name" required>
                      </div>
                      <div class="form-group">
                          <input type="text" class="form-control" id="lastName" ng-model="lname" placeholder="Last Name" required>
                      </div>
                      <div class="form-group">
                          <input type="email" class="form-control" id="email" ng-model="email" placeholder="Email Address" required>
                      </div>
                      <div class="form-group">
                          <input type="text" class="form-control" id="phone" ng-model="phone" placeholder="Phone" required>
                      </div>
                  
                  <button type="submit" class="btn btn-info" ng-click="submit()">Submit</button>
                  
                  </form>
                  </div>
                  <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.5.8/angular.min.js"></script>
                  <script src="controllers/controller.js"></script>
                  
                  </body>
                  </html>
                  

                  server.js

                  var express    = require("express");
                  var app = express();
                  var bodyParser = require('body-parser');
                  var mysql      = require('mysql');
                  var connection = mysql.createConnection({
                     host     : 'localhost',
                     user     : 'root',
                     password : '',
                     database : 'bookitnow'
                  });
                  
                  connection.connect(function(err) {
                     if (!err)
                       console.log('Database is connected');
                     else
                       console.log('DB connection error.');
                  });
                  
                  app.use(express.static(__dirname + '/public'));
                  
                  app.get('/index.html', function(req, res){
                      res.sendFile(__dirname + '/public' + 'index.html');
                  });
                  
                  
                  app.post('/index.html', function(req, res, next) {
                    var data = req.body;
                      console.log('request received:', data);
                  
                      connection.query('INSERT INTO register SET ?', data, function(err,res){
                        if(err) throw err;
                          console.log('record inserted');
                      });  
                  
                  app.listen(5500);
                  
                  console.log('Hi from server.js');
                  

                  controller.js

                  var app = angular.module('bookitnow',[]);
                          app.controller('myCtrl', function($scope,$http){
                          $scope.submit = function() {
                  
                              var data = {
                                      fName   : $scope.fname, 
                                      lName   : $scope.lname,
                                      email   : $scope.email,
                                      phone   : $scope.phone
                                  }   
                              };
                  
                              $http.post('/index.html', &scope.data)
                              .success(function (data, status, headers, config) {
                                      $scope.result = data;
                                      console.log("inserted successfully")
                                  })
                                  .error(function(data, status, header, config){
                                      $scope.result = "Data: " + status;
                  
                  
                                  });
                  

                  解决方案

                  In your controller.js, try the following and see what happens.

                  var app = angular.module('bookitnow',[]);
                  app.controller('myCtrl', function($scope, $http){
                      $scope.submit = function() {
                  
                          var data = {
                              fName   : $scope.fname, 
                              lName   : $scope.lname,
                              email   : $scope.email,
                              phone   : $scope.phone
                          }; 
                  
                          $http
                              .post('/index.html', data)
                              .then(function (response) {
                                  console.log(response)
                              }, function(response){
                                  console.log(response)
                              });
                       };
                  });
                  

                  这篇关于如何使用 MEAN 堆栈(MySQL、Express、Angular、Node)将数据插入数据库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                  上一篇:如何在 Ionic 框架中使用 ngCordova s​​qlite 服务和 下一篇:角度错误 - ReferenceError: $modal 未定义

                  相关文章

                  最新文章

                  1. <legend id='JcSkP'><style id='JcSkP'><dir id='JcSkP'><q id='JcSkP'></q></dir></style></legend>

                    <small id='JcSkP'></small><noframes id='JcSkP'>

                    <tfoot id='JcSkP'></tfoot>

                      <bdo id='JcSkP'></bdo><ul id='JcSkP'></ul>

                    1. <i id='JcSkP'><tr id='JcSkP'><dt id='JcSkP'><q id='JcSkP'><span id='JcSkP'><b id='JcSkP'><form id='JcSkP'><ins id='JcSkP'></ins><ul id='JcSkP'></ul><sub id='JcSkP'></sub></form><legend id='JcSkP'></legend><bdo id='JcSkP'><pre id='JcSkP'><center id='JcSkP'></center></pre></bdo></b><th id='JcSkP'></th></span></q></dt></tr></i><div id='JcSkP'><tfoot id='JcSkP'></tfoot><dl id='JcSkP'><fieldset id='JcSkP'></fieldset></dl></div>