2016-08-05 9 views
0

私はウェブ開発には新しく、解決できない問題に遭遇しました。私は今までMEAN -Stackで始まっていました。次の問題が発生しました。MEAN-Stackはmongooseを使って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' } 

     ] 
     } 
    } 
]; 

スニペットこの目的のために、私は同じ角度のコントローラに次のスニペットを持っています

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

私の知る限り理解し、このスニペットは

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); 

server.model.jsファイルに定義され、次のスキーマを使用して、私のデータベースに新しい記事エントリを作成しない。しかし、私の問題は今、これが唯一の空を節約するということです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
はこれにあなたのスキーマを変更し

、それが動作するはずです:私はちょうどスキーマ内[]を指定することで、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' 
    }, 
    alert: [] 
}); 


mongoose.model('Article', ArticleSchema); 

、その後、あなたは、アレイ内の複数のオブジェクトを保存することができます。

+0

MongoDBの入力はまだ空の配列です。あるいは 'alert:this.alert 'を何とか変更する必要がありますか? – danielkr

+0

ありがとうございました。それは配列で動作します。この特定の場合の問題は、 – danielkr

+0

を動作させるためには、 'alerts:[]'と 'alerts:this.alerts'でなければならないということです。私はこれを見つける前に、ほぼ同じ問題で苦労していました。ありがとうございました! –

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'); 
    } 
}); 

を保存

+0

codeGigは、私はmongooseにデータを保存することができますが、残念ながらアラートの配列は空であり、私が言及した値で設定されていません – danielkr

0

あなたがMEAN、その後の保存データを使用している場合は、エクスプレス、MongoDBの(マングース)とNodejsのハンドルであるサーバー上に来て、参照用mongoose save documentationです。

私はあなたがあなたの角度$ HTTP AJAXによって呼び出されたルート ルートにする必要があるよ

まずのMeanjs使用していると仮定しています: 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を使用しています。しかし、私はすでにあなたが私のプロジェクトで言及している、すべてのコードをyeomanジェネレータが生成しました。私はmongooseにデータを書き込むことができますが、mongooseのデータは空のアラート配列で構成されています。 – danielkr

関連する問題