社区所有版块导航
Python
python开源   Django   Python   DjangoApp   pycharm  
DATA
docker   Elasticsearch  
aigc
aigc   chatgpt  
WEB开发
linux   MongoDB   Redis   DATABASE   NGINX   其他Web框架   web工具   zookeeper   tornado   NoSql   Bootstrap   js   peewee   Git   bottle   IE   MQ   Jquery  
机器学习
机器学习算法  
Python88.com
反馈   公告   社区推广  
产品
短视频  
印度
印度  
Py学习  »  MongoDB

如何从EJS、Javascript写入MongoDB数据库?

Banana Cake • 4 年前 • 1024 次点击  

我是web/server开发的新手,对从ejs调用代码有点困惑。我有一张桌子,我想每一排都有按钮。我希望在点击按钮之后,它将从MongoDB中删除特定的项(我使用的是Mongoose、NodeJS、Bootstrap Table、EJS)。 这是我的密码和按钮

'<a class="remove" href="javascript:void(0)" title="Remove">',
        '<i class="fa fa-trash"></i>',
        '</a>' 

<table id="table">
    <thead>
      <tr>
        <th data-field="name">Device name</th>
        <th data-field="receivingKey">Receiving key</th>
        <th data-field="operate" data-formatter="operateFormatter" data-events="operateEvents"></th>
      </tr>
    </thead>
  </table>

  <script>
    var $table = $('#table');
    var data = <%- JSON.stringify(devices) %>;

    function operateFormatter(value, row, index) {
      return [
        '<a class="like" href="javascript:void(0)" title="Like">',
        '<i class="fa fa-heart"></i>',
        '</a>  ',
        '<a class="remove" href="javascript:void(0)" title="Remove">',
        '<i class="fa fa-trash"></i>',
        '</a>'
      ].join('')
    }

    window.operateEvents = {
      'click .like': function (e, value, row, index) {
        alert('You click like action, row: ' + JSON.stringify(row))
      },
      'click .remove': function (e, value, row, index) {
        // I want the delete action here.
      }
    }

    $(function () {
      $('#table').bootstrapTable({ data: data });
    });
  </script>
  <% } else { %>
  <div>You don't have any connected devices.</div>
  <% } %>
</div>

问题是,我不知道怎么做。我可以在nodejs后端编写代码,但我不知道如何从EJS调用它。我尝试使用app.local将函数传递到那里,但它由于内部的异步调用而引发错误。

如果你认为这段代码不好,我可以用一些不同的东西,让我也知道。谢谢您。

Python社区是高质量的Python/Django开发社区
本文地址:http://www.python88.com/topic/54752
 
1024 次点击  
文章 [ 4 ]  |  最新文章 4 年前
Banana Cake
Reply   •   1 楼
Banana Cake    5 年前

谢谢大家的帮助。我没有意识到我可以使用POST方法而不需要呈现或重定向到某个页面。我还发现AJAX可以很容易地发送POST请求。

后端:

const express = require('express');
const router = express.Router();

router.post('/remove-device', (req, res) => {
    //some code for deleting from mongo DB
    console.log('Success');
    res.send('ok');
});

module.exports = router;

前端:

<table id="table">
        <thead>
          <tr>
            <th data-field="name">Device name</th>
            <th data-field="receivingKey">Receiving key</th>
            <th data-field="operate" data-formatter="operateFormatter" data-events="operateEvents"></th>
          </tr>
        </thead>
      </table>

      <script>
        var $table = $('#table');
        var data = <%- JSON.stringify(devices) %>;

        function operateFormatter(value, row, index) {
          return [
            '<a class="like" href="javascript:void(0)" title="Like">',
            '<i class="fa fa-heart"></i>',
            '</a>  ',
            '<a class="remove" href="javascript:void(0)" title="Remove">',
            '<i class="fa fa-trash"></i>',
            '</a>'
          ].join('')
        }

        window.operateEvents = {
          'click .like': function (e, value, row, index) {
            alert('You click like action, row: ' + JSON.stringify(row))
          },
          'click .remove': function (e, value, row, index) {
            $.ajax({
              method: "POST",
              url: "/intr/remove-device",
              data: { deviceName: row.name },
            }).done(function (data) {});
          }
        }

        $(function () {
          $('#table').bootstrapTable({ data: data });
        });
THEWOLF
Reply   •   2 楼
THEWOLF    5 年前

对于这个问题,答案不能这样给出。。。我能做的就是引导你用正确的方法来学习这项技术

here 您将看到什么是express以及如何使用它来配置支持的API。。

here 你将进一步了解这个完整的堆栈,以及它是如何随着每一项技术完美地下降的。。这是Brad traversy的教程。。我也从他那里学到了。。

干杯伙计:)

Reinstate Monica
Reply   •   4 楼
Reinstate Monica    5 年前

澄清一下,ejs是

ejs用于:

  • 将变量从服务器传递到html文件

做你想做的事,你需要 请求 从javascript文件到app.js页面上设置的特定路由,然后从服务器向db发出请求。假设您正在使用express,请按照以下步骤操作。如果您不使用express,请告诉我,我可以指导您完成设置。

首先,您需要页面底部的jQuery脚本:

<script src="https://code.jquery.com/jquery-3.3.1.min.js"></script> 

<!-- make sure this script is above all your other scripts! -->

$.ajax({url:'/somePathHere', success: function(data) {

    //code you want to execute in the clients browser after the request is made goes here

}});

最后,在app.js页面上:

app.get('/somePathHere', function(req, res) {
    //make your call to the db here
}):