angularjs - Mongoose 架构中具有默认值的属性不会持久存在

标签 angularjs node.js mongodb express mongoose

我使用 Mongoose 编写了以下架构:

var querySchema = mongoose.Schema({
    quoteId: { type: String, default: '' },
    zipcode: { type: String, default: '' },
    email: { type: String, default: '' },
    type: {type: String, default: ''},
    isEmailChecked: { type: Boolean, default: true },
});

我只为 querySchema 中的 3 个属性提供值,假设当持久化 query 对象的新实例时,字段的结果将采用默认值:

var query = {};
query.quoteId = "1414775421426";
query.email = "myself@somewhere.com";
query.type = "Foo";

但是以下文档是我在集合中看到的结果:

{
    "_id" : ObjectId("5453c27d0e4c3f2837071856"),
    "email" : "myself@somewhere.com",
    "type" : "Foo",
    "quoteId" : "1414775421426",
    "__v" : 0
}

query 对象的新实例保存到 MongoDB 数据库时,是否应该为 isEmailCheckedzipcode 分配默认值?

以下是我如何使用 ExpressJS/NodeJS 持久保存查询对象的实例:

app.post('/api/queries', function (req, res) {
    QuoteQuery.create({
        quoteId: req.body.query.quoteId,
        type: req.body.query.type,
        zipcode: req.body.query.zipcode,
        email: req.body.query.email,
        isEmailChecked: req.body.query.isEmailChecked,
    }, function (err, query) {
        if (err) {
            res.send(err);
        }
        res.json(query);
    });
});

有人可以帮助我理解为什么我在 MongoDB 数据库的结果文档中获得了 isEmailCheckedzipcode 属性吗?

我在我的应用程序中使用 NodeJS、AngularJS 和 ExpressJS 以及 MongoDB。

最佳答案

当您设置 Mongoose 模型字段时,它不使用默认值。

作为解决方法,您可以使用下划线使用 query 对象中存在的键来扩展 mongoose 模型对象,如下所示:

_.extend(dbQueryObject, query);

这是完整的示例:

var mongoose = require('mongoose');

var querySchema = mongoose.Schema({
  quoteId: { type: String, default: '' },
  zipcode: { type: String, default: '' },
  email: { type: String, default: '' },
  type: {type: String, default: ''},
  isEmailChecked: { type: Boolean, default: true }
});

var db = mongoose.createConnection('mongodb://localhost:27017/stackoverflow',
  { server: { auto_reconnect: true } },
  function(err) {
    var QuerySchema = db.model('test', querySchema);

    var query = {};
    query.quoteId = "1414775421426";
    query.email = "myself@somewhere.com";
    query.type = "Foo";

    QuerySchema.create({
      quoteId: query.quoteId,
      type: query.type,
      zipcode: query.zipcode,
      email: query.email,
      isEmailChecked: query.isEmailChecked
    }, function (err, query) {
      process.exit(0);
    });
  });

这是数据库中的内容:

{
    "_id" : ObjectId("5453ce3c9f7e0d13c52abf61"),
    "type" : "Foo",
    "email" : "myself@somewhere.com",
    "quoteId" : "1414775421426",
    "__v" : 0
}

关于angularjs - Mongoose 架构中具有默认值的属性不会持久存在,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26680441/

相关文章:

javascript - 第一个响应中缺少 Access-Control-Allow-Origin

javascript - AngularJS 面试问题/挑战

node.js - 有没有一个库可以在node.js中实现pusher.com?

python - 仅使用 python-eve API 向 MongoDB 请求 `_items`

javascript - Sails 政策 - 通过协会搜索

javascript - NodeJS MongoDB 查找早于当前日期的帖子

javascript - 如何在 Firefox 调试器中找到该变量的值?

angularjs - UI-路由器 AngularJs : Prevent calling parent state controller when transitioning to child state

node.js:导入模块的范围

javascript - 如何将 Django API 项目与 Nodejs 集成并在前端使用react?