javascript - 从 ejs View 获取 JSON 数据到 Controller

标签 javascript json node.js express ejs

这是我的 server.js,

const express = require("express");
var app = express();
var cons = require('consolidate');
const bodyParser = require('body-parser');

app.use(bodyParser.urlencoded({ extended: false }));
app.use(bodyParser.json());
app.engine('html', cons.ejs);
app.set('view engine', 'html');

app.get("/getAllData", function(req, res) {
    GPSDataSchemaModel.find({}, function(err, result) {
        if (err) throw err;
        if (result) {
            res.render("index.ejs", {
                dbData: result
            });
        } else {
            res.send(JSON.stringify({
                error: 'Error'
            })
        )}
    })
})

app.get("/onmap", function(req, res) {
    console.log("Query type: " + typeof(req.query))
    console.log("Body type: " + typeof(req.body))
    console.log("Parameters type: " + typeof(req.params))
    res.send(req.query)
})

这是我的index.ejs,

<table class="table table-striped table-bordered" id="myTable">
                    <thead>
                        <tr>                          
                            <td>In Map</td>
                        </tr>
                    </thead>
                    <tbody>
                    
                     <% for(var i=0; i < dbData.length; i++) { %>                        
                     <tr>
                     <td><a href="/onmap?<%= JSON.stringify(dbData[i]) %>" class="btn btn-info" role="button">Map</a></td>  
                    </tr>
                    <% } %>

                    </tbody>
                </table>

mongoDb中的数据格式如下,

{
"_id": {
    "$oid": "58ad111590c50e1ca0c1f352"
},
"deviceID": "group-6-GPS",
"deviceTime": "04:41:37",
"deviceDate": "2016/06/07",
"receiveTime": "10:18:29",
"receiveDate": "2017/02/22",
"latitude": 17.08,
"longitude": 15.07,
"fix": 0,
"satellites": 0,
"altitude": null,
"deviceSpeed": 0,
"devicecourse": 0,
"deviceVariation": null,
"__v": 0
}

点击 map 按钮后,我得到这样的数据,

{
{"_id":"58ad111590c50e1ca0c1f352","deviceID":"group-6-GPS","deviceTime":"04:41:37","deviceDate":"2016/06/07","receiveTime":"10:18:29","receiveDate":"2017/02/22","latitude":"17.08","longitude":"15.07","fix":0,"satellites":0,"altitude":null,"deviceSpeed":0,"devicecourse":0,"deviceVariation":null,"__v":0}: ""
}

请注意以上两者的数据格式不相同。

我需要从第二个数据访问纬度经度。我怎样才能做到这一点?或者,我可以做什么来获取 JSON 格式的第二个数据?

最佳答案

如果您只想在查询中提供纬度和经度,那么下面的代码提供了 url

http://localhost:3000/onmap?latitude=17.08&longitude=15.07

解析后的查询将为{"latitude":"17.08","longitude":"15.07"}

<table class="table table-striped table-bordered" id="myTable">
<thead>
    <tr>                          
        <td>In Map</td>
    </tr>
</thead>
    <tbody>
        <% for(var i=0; i < dbData.length; i++) { %>                        
        <tr>
            <td><a href="/onmap?latitude=<%= dbData[i].latitude %>&longitude=<%= dbData[i].longitude %>" class="btn btn-info" role="button">Map</a></td>  
        </tr>
        <% } %>
    </tbody>
</table>

关于javascript - 从 ejs View 获取 JSON 数据到 Controller ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42593932/

相关文章:

android - 改造 |解析嵌套的 JSON 对象(“预期为 BEGIN_ARRAY 但 BEGIN_OBJECT 位于...)

json - MongoDB Compass 导出包含不需要的元数据的集合

javascript - nextProps 键虽然有内容但神秘地无法访问

javascript - 使用 ES6 导入模块和 pkg

Javascript:为什么我会收到此 Uncaught TypeError:无法读取 null 的属性 'addEventListener'?

javascript - TypeScript 不知道组件从 HOC 获取了 required prop

node.js - 除了少数异常(exception),如何使用 rimraf 删除所有内容

node.js - 当我更改 fieldName 并包含它时,Sequelize 会进行错误的查询

javascript - 在reactjs中渲染嵌套对象

javascript - 如何使用 jQuery 将 <a></a> 标签附加到元素中的特定单词