2016-08-05 74 views
0

我是網絡開發新手,我遇到了一個我無法解決的問題。我從MEAN開始,到目前爲止,我遇到了以下問題。MEAN-Stack使用貓鼬在MongoDB中保存數組

我在我的角度js控制器中有一個滑塊的數組結構。從這個數組結構中,我試圖將一些值保存到我的數據庫中。

角JS片斷控制器

$scope.alerts = [ 
    { id: 1, 
     title: 'CustomerCount', 
     value: 1, 
     weight: 30, 
     options: { 
     showTicks: true, 
     hidePointerLabels: true, 
     hideLimitLabels: true, 
     stepsArray: [ 
      { value: 1, legend: '<10' }, 
      { value: 2, legend: '<50' }, 
      { value: 3, legend: '<250' }, 
      { value: 4, legend: '<500' }, 
      { value: 5, legend: '>500' } 

     ] 
     } 
    }, 
    { id: 2, 
     title: 'CustomerSatisfaction', 
     value: 3, 
     weight: 40, 
     options: { 
     showTicks: true, 
     hidePointerLabels: true, 
     hideLimitLabels: true, 
     stepsArray: [ 
      { value: 1, legend: '<10' }, 
      { value: 2, legend: '<50' }, 
      { value: 3, legend: '<250' }, 
      { value: 4, legend: '<500' }, 
      { value: 5, legend: '>500' } 

     ] 
     } 
    } 
]; 

從上面的代碼段中,我想保存標題,所述和警告中

對於每個警報的重量這個目的,我在相同的角度js控制器下面的片段

// Create new Article object 
    var article = new Articles({ 
    title: this.title, 
    alert: this.alert, 
    }); 

據我瞭解沒有這個片段用在server.model.js文件中定義了以下模式創建我的數據庫一個新的文章進入

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

/** 
* Article Schema 
*/ 
var ArticleSchema = new Schema({ 
    created: { 
    type: Date, 
    default: Date.now 
    }, 
    user: { 
    type: Schema.ObjectId, 
    ref: 'User' 
    }, 
    alert: [{ value: Number, weight: Number, title: String }] 
}); 


mongoose.model('Article', ArticleSchema); 

但我現在的問題是,這不僅節省了空數組在MongoDB中。

下面介紹的解決方案導致解決方案。必須注意的是,

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

/** 
* Article Schema 
*/ 
var ArticleSchema = new Schema({ 
    created: { 
    type: Date, 
    default: Date.now 
    }, 
    user: { 
    type: Schema.ObjectId, 
    ref: 'User' 
    }, 
    alerts: [] 
}); 

    // Create new Article object 
    var article = new Articles({ 
    title: this.title, 
    alerts: this.alerts, 
    }); 

交付通緝的解決方案!

+1

你在模型上調用'save()'方法的位置,即'article.save()'? – chridam

+0

我在我的文章article.server.controller.js文件,我沒有在這裏列出,但它看起來像下面提供的一個codeGig – danielkr

回答

1

您的架構改成這樣,它應該工作:

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

/** 
* Article Schema 
*/ 
var ArticleSchema = new Schema({ 
    created: { 
    type: Date, 
    default: Date.now 
    }, 
    user: { 
    type: Schema.ObjectId, 
    ref: 'User' 
    }, 
    alert: [] 
}); 


mongoose.model('Article', ArticleSchema); 

我設法只是在指定的模式[]保存在MongoDB的數組,然後你可以在陣列中保存多個對象。

+0

沒有辦法,MongoDB中的輸入仍然是一個空數組。或者我需要改變'alert:this.alert,'這個不知何故? – danielkr

+0

謝謝,幫助了很多!它適用於數組。在這個特定情況下,問題在於它必須是'alerts:[]'和'alerts:this.alerts'才能工作 – danielkr

+0

這對我也有幫助。在我發現這一點之前,我一直在爲同樣的問題奮鬥了近一個星期。謝謝! –

0

您需要在Article模式上調用save方法。只有這樣,文章被保存

var article = new Articles({ 
title: this.title, 
alert: this.alert, 
}); 

article.save(function(err, doc){ 
    if(!err){ 
    // Article saved! "doc" is the reference 
    }else{ 
    console.error('Error on saving the article'); 
    } 
}); 

這裏是mongoose save documentation參考

+0

由於codeGig聲明是已​​在我的項目中提到的代碼。我能夠將數據保存到貓鼬,但不幸的是,警報數組是空的,沒有填寫我提到的值 – danielkr

0

如果您正在使用MEAN然後保存數據來自於服務器是處理上快速,MongoDB的(貓鼬)和的NodeJS。

我假設你正在使用喲

首先Meanjs你需要做一個其中由您的角度$ HTTP AJAX 路線稱爲路線: article.server.route.js

// Articles collection routes 
    app.route('/api/articles') 
    .post(articles.create); 

控制器: article.server.controller.js

exports.create = function (req, res) { 
    var article = new Article(req.body); 
    article.user = req.user; 

    article.save(function (err) { 
    if (err) { 
     return res.status(400).send({ 
     message: err 
     }); 
    } else { 
     res.json(article); 
    } 
    }); 
}; 
+0

我正在使用Mean js,並且我使用yo生成了該項目。但是我已經完成了您在我的項目中提到的由yeoman生成器生成的所有代碼。我可以將數據寫入貓鼬中,但貓鼬中的數據由一個空警報數組組成,而不是由上面定義的值填充。 – danielkr