如何使用三种模式从{mongoose模型访问数据

时间:2018-02-21 15:47:21

标签: node.js mongodb express mongoose-schema

您好我需要帮助从我的路由中的mongoose模式访问某些数据,以便在UI上呈现它。我有3个像这样的对象的json文件,

{
  "name": "1_test_spec.rb",
  "folder": "working_1",
  "test": "Test 1 TC 134389 Add to Autopay with Visa"
}

我将json文件导入到我的本地mongoDB,每个文件都有自己的集合。名称格式为test_working1 / 2/3。我创建了以下架构。

const mongoose = require("mongoose");

//SCHEMA SETUP
const test_working1Schema = new mongoose.Schema({
  name: String,
  folder: String,
  test: String
});

const test_working2Schema = new mongoose.Schema({
  name: String,
  folder: String,
  test: String
});

const test_working3Schema = new mongoose.Schema({
  name: String,
  folder: String,
  test: String
});

module.exports = mongoose.model("Test_working1", test_working1Schema);
module.exports = mongoose.model("Test_working2", test_working2Schema);
module.exports = mongoose.model("Test_working3", test_working2Schema);

在我的.js文件的顶部,我需要架构Test = require("../models/test"),我的路由设置如此,

router.get("/", (req, res) => {
  Test.find({}, (err, allTests) => {
    if(err){
        console.log(err);
    }  else {
      res.render("automation/index", { test: allTests, username: req.user });
    }
  });
});



router.get("/:id/test", (req, res) =>{
  Test.findById(req.params.id, (err, foundTest) => {
    let folder = foundTest.folder;
    let name = foundTest.name;
    let test = foundTest.test;

      if(err){
          console.log(err);
      } else {
        ChildProcess.exec(`cd ${path} & cd ../../ & cd ./automation/WSS_Automation & rspec ./spec/tests/` + folder + "/" + name, (error, stdout, stderr) => {
        console.log('stdout: ' + stdout);
        console.log('stderr: ' + stderr);
        console.log('error: ' + error);
        });
        req.flash("added", test + " " + "is running!" );
        res.redirect("/QAApplicationHub/Automation/");
        // res.send("WSS " + test + ' ' + "is running!");
      }
  });
});

上述路由处理比较每个测试所在的文件夹的名称以及用户通过比较ID选择运行的测试。我不是使用EJS来调用UI中的数据,

<select class="form-control col-md-4 slct001">
        <option value="">Choose one...</option>
        <% test.forEach(function(test){ %>
        <option value="/QAApplicationHub/Automation/<%= test._id %>/test"><%= test.test %></option>
        <% }); %>
      </select>

这里的问题是它只显示来自test_working3的测试文件。目标是有三个单独的选择下拉菜单。每个select元素都包含每个文件夹的测试。用户将选择他们想要运行的测试,并选择一个按钮来运行自动化测试脚本。

我相信有更有效的方法可以做到这一点。我只熟悉一次使用一个模式和路由对,所以这比我曝光的还要多一点。任何帮助表示赞赏。感谢。

更新

我将我的架构更新为

const mongoose = require("mongoose");

//SCHEMA SETUP
const test_working1Schema = new mongoose.Schema({
  name: String,
  folder: String,
  test: String
});

const test_working2Schema = new mongoose.Schema({
  name: String,
  folder: String,
  test: String
});

const test_working3Schema = new mongoose.Schema({
  name: String,
  folder: String,
  test: String
});


const Test1 = mongoose.model("Test_working1", test_working1Schema);
const Test2 = mongoose.model("Test_working2", test_working2Schema);
const Test3 = mongoose.model("Test_working3", test_working3Schema);

module.exports = { Test1, Test2, Test3 }

我把它包含在我的.js文件中,就像这样,

const Test1          = require("../models/test").Test1,
      Test2          = require("../models/test").Test2,
      Test3          = require("../models/test").Test3;

我的路线看起来像这样,

router.get("/", (req, res) => {
  Test1.find({}, (err, allTests) => {
    if(err){
        console.log(err);
    }
      else {
      res.render("automation/index", { test1: allTests, username: req.user 
     });
    }
  });
});

有没有办法从一条路线传递每个模型,类似于我如何使用Test1.find({} .....或者应该在模式内处理?

好吧,为了让事情变得简单,我希望我将json文件和架构的结构更改为正确的格式。所以现在我的Json文件就像这样

{"working1": [{

  "name": "1_test_spec.rb",
  "folder": "working_1",
  "test": "Test 1 TC 134389 Add to Autopay with Visa"
},
  {
  "name": "2_test_spec.rb",
  "folder": "working_1",
  "test": "Test 2 TC 134389 Add to Autopay with Master Card"
  }
  ]}

  {"working2": [{

    "name": "11_test_spec.rb",
    "folder": "working_2",
    "test": "Test 11 TC 149627 Create a Confirmed Reservation"
  },
    {
      "name": "12_test_spec.rb",
      "folder": "working_2",
      "test": "Test 12 TC 149630 Create Online Reservation"
    }
    ]}

    {"working3": [{
        "name": "21_test_spec.rb",
        "folder": "working_3",
        "test": "Test 21 TC 161117 Move Out with visa Card Refund"
      },
      {
        "name": "22_test_spec.rb",
        "folder": "working_3",
        "test": "Test 22 TC 161117 Move Out with Cash Refund"
      }
      ]}

我将模式更新为此,

const mongoose = require("mongoose");

//SCHEMA SETUP
const testSchema = new mongoose.Schema({
  working1: [{ name: String, folder: String, test: String}],
  working2: [{ name: String, folder: String, test: String}],
  working3: [{ name: String, folder: String, test: String}]

});

module.exports = mongoose.model("Test", testSchema);

这应该可以更容易地将我想要的数据放入每个选择元素下拉?

2 个答案:

答案 0 :(得分:0)

之所以发生这种情况,是因为在使用架构的文件中,只有最后一行有效。即,

module.exports = mongoose.model("Test_working3", test_working2Schema);

覆盖早期的出口。

一个文件只能以您的方式导出一个模型,并且是执行此操作的标准方法。 如果您真的想从同一个文件中导出所有三个模型,可以执行以下操作:

module.exports = {
    test1 : mongoose.model("Test_working1", test_working1Schema),
    test2 : mongoose.model("Test_working2", test_working2Schema);
    test3 : mongoose.model("Test_working3", test_working2Schema);
}

使用

并要求所有3个模式
var Test1 = require("../models/test").test1
var Test2 = require("../models/test").test2
var Test3 = require("../models/test").test3

然后,分别从每个中的3个渲染数据。像这样:

var async = require('async')
router.get("/", (req, res) => {
    async.parallel({
        test1: function (callback1) {
            Test1.findOne({}, callback1)
        },
        test2: function (callback2) {
            Test2.findOne({}, callback2)
        },
        test3: function (callback3) {
            Test3.findOne({}, callback3)
        },

    }, function (err, results) {
        if(err){
            console.log(err);
        } else {
            var tests = test1.concat(test2).concat(test3);
            res.render("automation/index", { test1: tests, username: req.user });
        }
    })
});

答案 1 :(得分:0)

这样做:

module.exports = mongoose.model("Test_working1", test_working1Schema);
module.exports = mongoose.model("Test_working2", test_working2Schema);
module.exports = mongoose.model("Test_working3", test_working2Schema);

我认为你只导出最后一个,因为你用不同的值覆盖导出,只有最后一个“幸存”。可能只会让你从第3个架构中获得结果。

我建议您做的是导出所需的方法,例如find方法和findById方法,并将每个方法实现到db文件中,在3个模型中搜索结果并返回所有方法。像这样:

const model1 = mongoose.model("Test_working1", test_working1Schema);
const model2 = mongoose.model("Test_working2", test_working2Schema);
const model3 = mongoose.model("Test_working3", test_working2Schema);

exports.find = async query => {
    let result1 = await model1.find(query).toArray(); 
    let result2 = await model2.find(query).toArray(); 
    let result3 = await model3.find(query).toArray(); 
    return [...result1, ...result2, ...result3];
}

exports.findById = async id => {
    // something similar
}