如何显示MongoDB属性?

时间:2017-08-05 04:37:56

标签: javascript mongodb mongoose electron

我在Mongo中托管了我的mLab.com数据库,其中包含多个集合,如下图所示:

enter image description here

我似乎无法访问“请求”集合。这就是我所做的:

首先,我连接到数据库并在主进程(main.js)中创建了该函数:

mongoose.connect('url', { useMongoClient: true });

ipcMain.on('load-requests', function(event) {
  return Requests.find({}, { sort: {createdAt: -1}});
});

在另一个名为schema.js的文件中,我有以下内容:

var mongoose     = require('mongoose');
var Schema       = mongoose.Schema;

var hosSchema   = new Schema({
    hospital: String,
    state: String,
    reasons: String,
    requestedDateTime: String,
    requestNumber: String,
    status: String,
});

module.exports = mongoose.model('Requests', hosSchema);

在渲染器进程(homePage.html)中,我有以下内容:

<div id="page-inner">
            <div class="row">
                <div class="col-md-4 col-sm-4">
                   <div class="card teal">
                        <div class="card-content white-text">
                          <span class="card-title">state</span>
                          <p>reason</p>
                        </div>
                        <div class="card-action">
                          <a href="#">requestNumber</a>
                          <a href="#">requestedDateTime</a>
                        </div>
                      </div>
                </div>
            </div>
         </div>

我想通过page-inner访问id,并在数据库中将属性更改为相关的一次。例如,应该使用从主进程(load-requests)中的函数检索的属性更改状态。

如何在homePage.html内显示属性?

1 个答案:

答案 0 :(得分:1)

在Schema.js中:

var hosSchemaModel = mongoose.model('Requests', hosSchema);
module.exports = hosSchemaModel;

在main.js中:

var hosSchemaModel = require("./Schema.js");
var getHosSchemas = function () {
    hosSchemaModel.find({}, function (err, hosSchema) {
          if (err) {
            //...
          } else {
            //Do something with the hosSchema 
        }
    });
}