node.js - 有没有办法在 Geddy 模型中定义虚拟场?

标签 node.js geddy

是否可以在geddy模型中定义临时字段/虚拟字段?

就像在表单中一样,我使用了输入字段 tmpFirstNametmpLastName但提交后我想将信息存储在单列 name 中.

谢谢

最佳答案

这可以通过新的生命周期方法轻松实现(感谢您!)。

在你的 Controller 中:

  this.create = function (req, resp, params) {
    var self = this
      , person = geddy.model.Person.create(params);

    person.firstname = params.firstname;
    person.lastname = params.lastname;

    if (!person.isValid()) {
      this.respondWith(person);
    }
    else {
      person.save(function(err, data) {
        if (err) {
          throw err;
        }
        self.respondWith(person, {status: err});
      });
    }
  };

在您的模型中:

  this.defineProperties({
    name: {type: 'string'}
  });

  this.beforeSave = function () {
    this.name = this.firstname + ' ' + this.lastname;
  }

请注意,不要声明“虚拟”属性,否则 geddy 会将它们存储在数据库中。

关于node.js - 有没有办法在 Geddy 模型中定义虚拟场?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19710305/

相关文章:

node.js - 如何保护nodejs中的公共(public)动态文件夹

node.js - 使用 Sequelize 和 DataTypes 作为数据类型

node.js - Geddy 单元测试 Controller

angularjs - 在 AngularJS 中加载图像目录

javascript - 在 Node.js Express/Connect 中,有没有办法将 session 设置为无限大?

node.js - 创建 geddy 应用程序后 geddy 命令中断

node.js - 如何解决 "customFds not yet supported"错误

node.js - GeddyJs(NodeJs 框架)部署到 heroku cedar 堆栈崩溃

node.js - 在heroku 上启动geddy 失败并出现错误R11(错误绑定(bind))。部署有什么问题?我缺少什么?

sql - 如何在 knex.js 中将连接表中的数据嵌套在一个(或多个)对多关系中?