0

I am new to web development and I just ran into a problem I can't solve. I started with the MEAN-Stack so far and I encountered the following problem.

I have an array structure for a slider in my angular js Controller. From this array structure, I am trying to save a few values into my database.

Angular JS Controller snippet

$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' }

        ]
      }
    }
];

From the above snippet, I'd like to save the title, the value and the weight of each alert in alerts

For this purpose, I have in the same angular js controller the following snippet

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

As far as I understand does this snippet create a new article entry in my database using the following schema defined in the server.model.js file

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

But my Problem now is that this only saves an empty array in MongoDB.

The solution presented below lead to the solution. It has to be noted that

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

Delivered the wanted solution!

2
  • 1
    Where are you calling the save() method on the model i.e. article.save()? Commented Aug 5, 2016 at 7:19
  • I have that in my article.server.controller.js file, which I did not list here but it looks like the one codeGig provided below Commented Aug 5, 2016 at 8:29

3 Answers 3

1

Change your schema to this and it should work:

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

I managed to save arrays in mongodb by just specifying [] in the schema and then you can save multiple objects in the array.

Sign up to request clarification or add additional context in comments.

3 Comments

No that way the input in MongoDB is still an empty array. Or do I need to change alert: this.alert, this somehow?
Thanks that helped a lot!!! It works with arrays. The problem in this specific case is that it has to be alerts: [] and alerts: this.alerts in order to work
This helped me as well. I was struggling with the same problem for almost a week before I found this. Thank you!
0

You need to call the save method on the Article schema. Only then the article is saved

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

Here is the mongoose save documentation for reference

1 Comment

As codeGig stated is the coded mentioned already in my project. I am able to save data to mongoose but unfortunately the alert array is empty and not populated with the values I mentioned
0

If You are using MEAN then Saving data comes on server which is handle on Express, Mongodb(Mongoose) and Nodejs.

I am assuming that you are using Meanjs of yo

First you need to make a route where which is called by your angular $http ajax Route: article.server.route.js

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

Controller: 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);
    }
  });
};

1 Comment

I am using Mean js and I generated the project with yo. But I have already all the code you mentioned in my project which was generated by the yeoman generator. I can write data into mongoose, but the data in mongoose consists of an empty alert array rather then a populated one with the above defined values.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.