sails.js – 嵌入式文档&有没有办法使用“type”关键字作为模型字段名称

这个问题看起来有点荒谬.但如果有办法,这对于字段名称标准化来说是很好的.

>我可以使用“type”关键字作为字段名称吗?

我已将字段名称更改为“方法”(另一种选择是“种类”).但它可能是地址的子文档字段.例如:

address: { 
     type: { 
              type: 'string'
           }
         }

我的模型如下;

payment: {
    type: {
        type: 'string'
    },
    tally_system: {
        installment_count: {
            type: 'integer'
        }
    },
    gift_card: {
        type: 'string'
    },
    total_amount: {
        type: 'integer'
    },
    discount_ratio: {
        type: 'integer'
    },
    total_amount_after_discount: {
        type: 'float'
    }
}

编辑

我可以在Sails中查询嵌入文档,如下所示.我认为,极有可能我可以在没有Waterline的情况下手动插入嵌入式文档并且很痛苦.
希望新的,Waterline提供嵌入式使用.

Bid.native(function(err, collection) {     
  collection
  .find({'_id' : req.param('id') })
  .nextObject(function (err, bid) {                        
     console.log(bid);
  });
});

最佳答案 Sails不支持嵌入式文档的模式,因此您无法执行以下操作:

tally_system: {
    installment_count: {
        type: 'integer'
    }
}

期望他们以你想要的方式工作.你能做的最好的事情是:

tally_system: {
    type: "json"
}

这将声明它是一个“json”字段,您可以将任意Javascript对象放入:

MyModel.create({ tally_system: [1,2,{abc:123}] })

话虽这么说,你可以有一个名为“类型”的字段,而不是一个问题.

点赞