我接近拥有一个有效的网络应用程序。 Mongoose正在发送我的数据 mongolab如果我在平面架构中有它没有问题。当我尝试 将它安排成更容易管理的东西,它作为一个空的到来 阵列(一个或多个)。我尝试过几种不同的架构设计 结果。我没有做任何过于花哨的事情。我看着对方 这里的帖子与空数组有关,但我唯一不能做的事情 弄清楚是通过submit()我的XHR请求发送数据时:
分辨
在我们开始之前,这对你来说可能无法正常工作,因为我使用平均堆栈以不同的方式完成所有导出操作。但是如何嵌入数组的原则仍应有效。
休息后吃点东西。我已更新原始帖子,以反映我在控制器和架构中的当前代码。我会在数据库中显示它的样子。对于任何想要在数组中放置数组的人来说:
在控制器内或您将表单数据绑定到文档的位置创建一个空数组,因此:
var followupDetails = [];
创建一个对象来表示要嵌入的数据(数组进入对象)
var followup = {
followup: [{
technicianName: this.technicianName,
consultationStatus: this.consultationStatus,
followUpDate: this.followUpDate,
followUpNotes: this.followUpNotes,
installDate: this.installDate,
technicianCellPhone: this.technicianCellPhone
}]
};
将对象推入空数组,因此:
followupDetails.push(后续);
将该属性添加到要将数组嵌入的现有对象中。我被称为咨询,我用followupDetails数组的值创建了一个 followup 属性。
consult.followup = followupDetails;
你不得不将它添加到创建文档的$ scope.create函数中,该函数将通过模式传递。它是在编译它的父级时创建的。即。
var ConsultationSchema = new Schema({
"consultation":{
"customerDetails": [customerDetailsSchema],
"currentSystem": [currentSystemSchema],
// followups in now inside the consultDetails
"consultDetails": [consultDetailSchema]
}
});
其他一切都完全一样。另请注意,我的后续生活咨询模式现在与以前相同:
var consultDetailSchema = new Schema({
...
...
followup:[followupSchema]
});
这是我整理的完整架构
'use strict';
/**
* Module dependencies.
*/
var mongoose = require('mongoose'),
Schema = mongoose.Schema;
var customerDetailsSchema = new Schema({
"customerName": {
type: String,
default: '',
trim: true
},
"customerAddress": {
type: String,
default: '',
trim: true
},
"customerCity": {
type: String,
default: '',
trim: true
},
"customerState": {
type: String,
default: '',
trim: true
},
"customerZipCode": {
type: Number,
default: ' ',
trim: true
},
"customerHomePhone": {
type: Number,
default: '',
trim: true
},
"customerCellPhone": {
type: Number,
default: '',
trim: true
},
"customerEmail": {
type: String,
default: '',
trim: true
},
"comporiumId":{
type:Number,
default:'',
trim:true
}
});
var currentSystemSchema = new Schema({
"systemType":{
type:String,
default:'',
trim:true
},
"systemOther":{
type: String,
default: '',
trim: true
},
"systemMonitored":{
type:String,
default:'',
trim:true
},
"deluxePhonePackage": {
type: Boolean
}
});
var followupSchema = new Schema({
"technicianName":{
type:String,
default:'',
trim:true
},
"consultationStatus":{
type:String,
default:'',
trim:true
},
"followUpDate":{
type:Date,
default:'',
trim:true
},
"followUpNotes":{
type:String,
default:'',
trim:true
},
"installDate":{
type:String,
default:'',
trim:true
},
"technicianCellPhone":{
type:Number,
default:'',
trim:true
}
});
var consultDetailSchema = new Schema({
"consultComments":{
type:String,
default:'',
trim:true
},
"consultantName":{
type:String,
default:'',
trim:true
},
"technician":{
type:String,
default:'',
trim:true
},
"scheduleDate":{
type:Date,
default:'',
trim:true
},
"created": {
type: Date,
default: Date.now
},
"user": {
type: Schema.ObjectId,
ref: 'User'
},
"followUpDetails": [followupSchema]
});
/**
* Consultation Schema
*/
var ConsultationSchema = new Schema({
"consultation":{
"customerDetails": [customerDetailsSchema],
"currentSystem": [currentSystemSchema],
"consultDetails": [consultDetailSchema]
}
});
mongoose.model('Consultation', ConsultationSchema);
这里的控制器,记得当你点击提交时它应该运行create()函数在实例化模型的新实例之前执行在create函数体内构建文档的代码:
'use strict';
// Consultations controller
angular.module('consultations')
.controller('ConsultationsController', ['$scope', '$stateParams', '$location', 'Authentication', 'Consultations',
function($scope, $stateParams, $location, Authentication, Consultations) {
$scope.authentication = Authentication;
// Create new Consultation
$scope.create = function() {
var customerDetails = [];
var systemDetails = [];
var consultDetails = [];
var followupDetails = [];
var customer = {
customerName: this.customerName,
customerAddress: this.customerAddress,
customerCity: this.customerCity,
customerState: this.customerState,
customerZipCode: this.customerZipCode,
customerHomePhone: this.customerHomePhone,
customerCellPhone: this.customerCellPhone,
customerEmail: this.customerEmail,
comporiumId: this.comporiumId
};
var system = {
systemType: this.systemType,
systemOther: this.systemOther,
systemMonitored: this.systemMonitored,
deluxePhonePackage: this.deluxePhonePackage
};
var followup = {
followup: [{
technicianName: this.technicianName,
consultationStatus: this.consultationStatus,
followUpDate: this.followUpDate,
followUpNotes: this.followUpNotes,
installDate: this.installDate,
technicianCellPhone: this.technicianCellPhone
}]
};
var consult = {
consultComments: this.consultComments,
consultantName: this.consultantName,
technician: this.technician,
scheduleDate: this.scheduleDate,
created: this.created,
user: this.user
};
customerDetails.push(customer);
systemDetails.push(system);
followupDetails.push(followup);
consultDetails.push(consult);
consult.followup = followupDetails;
// Create new Consultation object
var consultation = new Consultations ({
consultation:{
customerDetails: customerDetails,
currentSystem: systemDetails,
consultDetails: consultDetails,
}
});
所以这就是我最初得到的:
{
"_id": {
"$oid": "556daad4da83801971345211"
},
"consultation": {
"consultDetails": [],
"currentSystem": [],
"customerDetails": []
},
"__v": 0
}
现在看来,db中没有传递数据,因为我只是单击“创建”而不是填充任何内容:
"_id": {
"$oid": "556e479f9695f6c804de0a7f"
},
"consultation": {
"consultDetails": [
{
"_id": {
"$oid": "556e479f9695f6c804de0a80"
},
"followup": [
{
"_id": {
"$oid": "556e479f9695f6c804de0a81"
},
"technicianCellPhone": null,
"installDate": "",
"followUpNotes": "",
"followUpDate": null,
"consultationStatus": "",
"technicianName": ""
}
],
"created": {
"$date": "2015-06-03T00:17:35.233Z"
},
"scheduleDate": null,
"technician": "",
"consultantName": "",
"consultComments": ""
}
],
"currentSystem": [
{
"_id": {
"$oid": "556e479f9695f6c804de0a82"
},
"systemMonitored": "",
"systemOther": "",
"systemType": ""
}
],
"customerDetails": [
{
"_id": {
"$oid": "556e479f9695f6c804de0a83"
},
"comporiumId": null,
"customerEmail": "",
"customerCellPhone": null,
"customerHomePhone": null,
"customerZipCode": 0,
"customerState": "",
"customerCity": "",
"customerAddress": "",
"customerName": ""
}
]
},
"__v": 0
}
答案 0 :(得分:0)
Mongoose并没有将一种数据格式神奇地转换为您的架构格式。你需要自己做。它只是在对象上强制执行您的架构。由于您的所有文档都没有required option且您不是strictly enforcing the schema,因此您实际上是在创建一个空文档(默认情况下会返回空的suddoc数组)。
因此,您需要更新处理发布请求的任何方法,以将请求post对象格式化为正确的格式以匹配您的Mongoose架构。您还可以将架构选项strict
设置为抛出而不是忽略(默认值),以确保您不会尝试向文档添加无效属性。之后,您应该能够检索非空文档。