邮差只发布到猫鼬嵌套级别1 [英] Postman only posts to mongoose nested level 1
问题描述
我正在尝试将帖子测试到具有嵌套字段的mongoose模式,但我只能将级别1和级别2中的第一个字段进行测试。例如:
I'm trying to test a post to a mongoose schema that has nested fields, but I'm only able to level 1 and the first field in level 2. For example:
我有一个可以包含多个IP地址和多个子网的网络模型。当我将查询放入Postman时,它允许我创建多个IP地址和多个子网(很棒),但我不能定义类型字段,例如?
I have a network model that can contain multiple ip addresses and multiple subnets. When I place the query in to Postman it allows me to create multiple ip addresses and multiple subnets (great) but I can't define a type field for example?
猫鼬架构:
var mongoose = require('mongoose'), Schema = mongoose.Schema, ObjectId = mongoose.Schema.ObjectId;
var networkSchema = module.exports = mongoose.model('Network', {
network_id:ObjectId,
location: String,
hostname: String,
device: String,
model: String,
ipAddress: [ipaddressSchema],
subnets: [subnetSchema],
iosVersion: String,
softwareImage: String,
serialNumber: String,
});
var ipaddressSchema = Schema ({
ipAddress: String,
type: String,
});
var subnetSchema = Schema ({
range: String,
type: String,
});
控制器:
var Network = require('../models/network');
module.exports.create = function (req, res) {
var network = new Network(req.body);
network.save(function (err, result) {
res.json(result);
});
}
module.exports.list = function (req, res) {
Network.find({}, function (err, results) {
res.json(results);
});
}
邮递员查询:
邮递员结果:
我想:
{
"__v": 0,
"location": "London Office",
"hostname": "lon-asa-01",
"device": "Switch-MLS",
"model": "Cisco 3750",
"softwareImage": "1.2",
"serialNumber": "123456",
"_id": "5510495c1d40ef965d7d1cec",
"subnets":[
["range" : "10.0.100.0/24", "type" : "Client" ],
["range" : "10.0.101.0/24", "type" : "Server" ],
],
"ipAddress": [
["ipAddress" : "10.0.100.1", "type" : "Inside" ],
["ipAddress" : "10.0.101.254", "type" : "Outside" ],
]
}
推荐答案
好的,你走了:
首先,你的计划a应如下所示:
First of all, your schema should look like this:
var networkSchema = module.exports = mongoose.model('Network', {
network_id: ObjectId,
location: String,
hostname: String,
device: String,
model: String,
ipAddress: [{type: ObjectId, ref: 'IpadressModelName'}],
subnets: [{type: ObjectId, ref: 'SubnetModelName'}],
iosVersion: String,
softwareImage: String,
serialNumber: String,
});
在您的控制器中,您必须首先插入您的网络所依赖的实体,这样您才能拥有_id提供给网络模型作为参考:
In your controller you have to insert first the entities on which your network relies on so you will have an _id to provide to the network model as reference:
module.exports.create = function (req, res) {
var network = new Network(req.body);
var ipAddress = [],
ipIds = [];
req.body.ipAddress.forEach(function(ip){
ipAddress.push(new IpadressModelName(ip));
});
var subnets = [],
subnetsIds = [];
req.body.subnets.forEach(function(sn){
subnets.push(new SubnetModelName(sn));
});
IpadressModelName.create(ipAddress, function () {
// args[0] should be the error
if (args[0]) {
throw args[0]
}else{
for(var i=1; i<args.length; i++ )
ipIds.push(args[i]._id);
}
SubnetModelName.create(subnets, function () {
// args[0] should be the error
if (args[0]) {
throw args[0]
}else{
for(var i=1; i<args.length; i++ )
subnetsIds.push(args[i]._id);
}
network.ipAddress = ipIds;
network.subnets = subnetsIds;
network.save(function (err, result) {
res.json(result);
});
});
});
}
最后将数据作为原始JSON发布:
Finally post data as raw JSON:
{
"location": "London Office",
"hostname": "lon-asa-01",
"device": "Switch-MLS",
"model": "Cisco 3750",
"softwareImage": "1.2",
"serialNumber": "123456",
"subnets":[
{"range" : "10.0.100.0/24", "type" : "Client" },
{"range" : "10.0.101.0/24", "type" : "Server" }
],
"ipAddress": [
{"ipAddress" : "10.0.100.1", "type" : "Inside" },
{"ipAddress" : "10.0.101.254", "type" : "Outside" }
]
}
此示例中的代码仅用于演示您可以使用的方法,并且不符合最佳实践。
The code in this example is just for demonstrating a approach you can use and it is not conforming to the best practices.
这篇关于邮差只发布到猫鼬嵌套级别1的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!