【问题标题】:MEAN-Stack save an array in MongoDB with mongooseMEAN-Stack 使用 mongoose 在 MongoDB 中保存一个数组
【发布时间】:2016-12-11 12:33:56
【问题描述】:

我是 Web 开发的新手,但遇到了一个我无法解决的问题。到目前为止,我从MEAN-Stack 开始,遇到了以下问题。

我的 Angular js 控制器中有一个滑块的数组结构。从这个数组结构中,我尝试将一些值保存到我的数据库中。

Angular JS 控制器 sn-p

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

        ]
      }
    }
];

从上面的sn-p,我想在alerts中保存每个alert的titlevalueweight

为此,我在同一个 Angular js 控制器中使用了以下 sn-p

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

据我了解,这个 sn-p 是否使用 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,
  });

提供了想要的解决方案!

【问题讨论】:

  • 你在哪里调用模型上的save()方法,即article.save()
  • 我的 article.server.controller.js 文件中有这个,我没有在这里列出,但它看起来像下面提供的一个 codeGig

标签: javascript angularjs mongodb mean-stack meanjs


【解决方案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 中,然后您可以在数组中保存多个对象。

【讨论】:

  • 不是这样,MongoDB 中的输入仍然是一个空数组。还是我需要以某种方式更改alert: this.alert,
  • 感谢您的帮助!!!它适用于数组。在这种特定情况下的问题是它必须是 alerts: []alerts: this.alerts 才能工作
  • 这对我也有帮助。在我发现这个问题之前,我已经为同样的问题苦苦挣扎了将近一个星期。谢谢!
【解决方案2】:

如果您使用的是 MEAN,则保存数据会出现在由 Express、Mongodb(Mongoose) 和 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);
    }
  });
};

【讨论】:

  • 我正在使用 Mean js,我用 yo 生成了项目。但是我已经有了你在我的项目中提到的由 yeoman 生成器生成的所有代码。我可以将数据写入 mongoose,但 mongoose 中的数据由一个空警报数组组成,而不是由上面定义的值填充的数组。
【解决方案3】:

您需要在 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供参考

【讨论】:

  • 正如 codeGig 所说的是我的项目中已经提到的编码。我可以将数据保存到猫鼬,但不幸的是警报数组是空的,并且没有填充我提到的值
猜你喜欢
  • 2017-08-30
  • 2014-06-05
  • 2015-08-25
  • 2023-04-10
  • 1970-01-01
  • 1970-01-01
  • 2014-09-07
  • 1970-01-01
  • 2020-10-22
相关资源
最近更新 更多