2016-08-05 2 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, 
    }); 
,210

지금까지 내가이 조각은 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
이에 스키마를 변경

와 그것을 작동합니다 : 난 그냥 스키마의 []를 지정하여 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는 이미 내 프로젝트에서 언급 한 코드입니다. 나는 몽구스에 데이터를 저장할 수 있지만 불행히도 경보 배열은 비어 있으며 내가 언급 한 값으로 채워지지 않았다. – danielkr

0

뜻 다음 저장 데이터를 사용하는 경우가 익스프레스, MongoDB를 (몽구스)와 Nodejs에 핸들 서버에 오는 참조 할 수 mongoose save documentation입니다.

난 당신이 요

최초의 Meanjs를 사용하고 당신이 당신의 각도 $ HTTP 아약스 경로에 의해 호출되는 경로를 만들 필요가 있다고 가정입니다 : 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

평균 js를 사용하고 있는데 나는이 프로젝트를 생성했다. 하지만 저는 이미 프로젝트에서 언급 한 모든 코드를 yeoman 생성기에서 생성했습니다. mongoose에 데이터를 쓸 수는 있지만, mongoose의 데이터는 위에 정의 된 값이있는 채워진 데이터가 아니라 빈 경고 배열로 구성됩니다. – danielkr

관련 문제