Sailsjs:不能在bootstrap上创建带有Date属性的虚拟对象

Sailsjs: can't create dummy object on bootstrap with Date attribute

本文关键字:Date 属性 对象 虚拟 创建 不能 bootstrap Sailsjs      更新时间:2023-09-26

我能够在添加"Date"属性之前创建一个虚拟用户对象。在bootstrap.js中我有:

module.exports.bootstrap = function(cb) {
    var dummyData = [
        {
            "firstName":"Jane",
            "lastName":"Doe",
            "dateofbirth": 1279703658 //timestamp
        }
    ]
    User.count().exec(function(err, count){
        if(err){
            return cb(err);
        }
        if(count == 0){
            User.create(dummyData).exec(function(){
                cb();
            });
        }
    });
};

User.js很简单,看起来像这样:

module.exports = {
  attributes: {
    firstName : {
        type : 'string',
        required : true
    },
    lastName : {
        type : 'string',
        required : true
    },
    dateofbirth : {
        type : 'date'
    }
  }
};

当我尝试在浏览器中创建相同的对象时(航行很好的CRUD功能),我得到一个关于日期的错误:

{
  "error": "E_VALIDATION",
  "status": 400,
  "summary": "1 attribute is invalid",
  "model": "User",
  "invalidAttributes": {
    "dateofbirth": [
      {
        "rule": "date",
        "message": "`undefined` should be a date (instead of '"123454345'",     which is a string)"
      }
   ]
 }
}

所以问题是我如何创建这样的对象与日期属性?

date(或等价的dateTime)接受例如ISO日期字符串。因此,您的示例看起来像:

var dummyData = [
        {
            "firstName":"Jane",
            "lastName":"Doe",
             // "1970-01-15T19:28:23.658Z"
            "dateofbirth": new Date(1279703658).toISOString() 
        }
    ]