Node.js 环回Api资源管理器创建错误:无效日期

Node.js 环回Api资源管理器创建错误:无效日期,node.js,loopback,Node.js,Loopback,我正在使用loopback的API explorer创建具有以下参数的模型: { "name": "string", "last_name": "string", "phone": 0, "is_invited": true, "realm": "string", "username": "string", "credentials": {}, "challenges": {}, "email": "string", "emailVerified": tr

我正在使用loopback的API explorer创建具有以下参数的模型:

{
  "name": "string",
  "last_name": "string",
  "phone": 0,
  "is_invited": true,
  "realm": "string",
  "username": "string",
  "credentials": {},
  "challenges": {},
  "email": "string",
  "emailVerified": true,
  "status": "string",
  "created": "2016-06-03",
  "lastUpdated": "2016-06-03",
  "id": 0
}
但是,服务器总是返回500无效日期错误:

{
  "error": {
    "name": "Error",
    "status": 500,
    "message": "Invalid date: Invalid Date",
    "stack": "Error: Invalid date: Invalid Date\n    at DateType }
}
这是我的模型供参考。它继承了环回的用户模型

{
  "name": "ExeboardUser",
  "base": "User",
  "idInjection": true,
  "options": {
    "validateUpsert": true
  },
  "properties": {
    "name": {
      "type": "string",
      "required": true
    },
    "last_name": {
      "type": "string"
    },
    "phone": {
      "type": "number",
      "required": true
    },
    "is_invited": {
      "type": "boolean",
      "required": true
    }
  },
  "validations": [],
  "relations": {
    "boards": {
      "type": "hasMany",
      "model": "Board",
      "foreignKey": "exeboardUserId",
      "through": "ExeboardUserBoard"
    }
  },
  "acls": [
    {
      "principalType": "ROLE",
      "principalId": "$everyone",
      "permission": "ALLOW",
      "property": "logout"
    }
  ],
  "methods": {

  }
}

谁能告诉我日期有什么问题吗?我认为它的格式正确,因为它是资源管理器生成的默认参数。

不知道您是否对答案感兴趣,但我最近开始使用loopback,得到了与您相同的错误,服务器接受的日期格式如下2017-01-06T23:58:10.000Z

希望它能帮助别人


注:如果模型不需要日期,甚至不要发送它,即使使用“null”或“”值,它也会抛出500状态错误。

不知道您是否对答案感兴趣,但我最近开始使用loopback,得到了与您相同的错误,服务器接受的日期格式如下2017-01-06T23:58:10.000Z

希望它能帮助别人


注:如果模型不需要日期,甚至不要发送,即使使用“null”或“”值,也会抛出500状态错误。

正确的日期格式是:2017-10-12T10:31:37.925Z

如果要添加dateCreated和dateUpdate字段,请使用DateMixin

使用安装mixin

npm i环回ds时间戳mixin--保存

将mixins属性添加到服务器/model-config.json:

{
"_meta": {
 "sources": [
   "loopback/common/models",
   "loopback/server/models",
   "../common/models",
   "./models"
 ],
 "mixins": [
   "loopback/common/mixins",
   "../node_modules/loopback-ds-timestamp-mixin",
   "../common/mixins"
 ]}
}
在您的模型中:

{
  "name": "ExeboardUser",
  "base": "User",
  "idInjection": true,
  "options": {
    "validateUpsert": true
  },
  "mixins": {
      "TimeStamp" : true
  },
  "properties": {
    "name": {
      "type": "string",
      "required": true
    },
    "last_name": {
      "type": "string"
    },
    "phone": {
      "type": "number",
      "required": true
    },
    "is_invited": {
      "type": "boolean",
      "required": true
    }
  },
  "validations": [],
  "relations": {
    "boards": {
      "type": "hasMany",
      "model": "Board",
      "foreignKey": "exeboardUserId",
      "through": "ExeboardUserBoard"
    }
  },
  "acls": [
    {
      "principalType": "ROLE",
      "principalId": "$everyone",
      "permission": "ALLOW",
      "property": "logout"
    }
  ],
  "methods": {

  }
}

正确的日期格式为:2017-10-12T10:31:37.925Z

如果要添加dateCreated和dateUpdate字段,请使用DateMixin

使用安装mixin

npm i环回ds时间戳mixin--保存

将mixins属性添加到服务器/model-config.json:

{
"_meta": {
 "sources": [
   "loopback/common/models",
   "loopback/server/models",
   "../common/models",
   "./models"
 ],
 "mixins": [
   "loopback/common/mixins",
   "../node_modules/loopback-ds-timestamp-mixin",
   "../common/mixins"
 ]}
}
在您的模型中:

{
  "name": "ExeboardUser",
  "base": "User",
  "idInjection": true,
  "options": {
    "validateUpsert": true
  },
  "mixins": {
      "TimeStamp" : true
  },
  "properties": {
    "name": {
      "type": "string",
      "required": true
    },
    "last_name": {
      "type": "string"
    },
    "phone": {
      "type": "number",
      "required": true
    },
    "is_invited": {
      "type": "boolean",
      "required": true
    }
  },
  "validations": [],
  "relations": {
    "boards": {
      "type": "hasMany",
      "model": "Board",
      "foreignKey": "exeboardUserId",
      "through": "ExeboardUserBoard"
    }
  },
  "acls": [
    {
      "principalType": "ROLE",
      "principalId": "$everyone",
      "permission": "ALLOW",
      "property": "logout"
    }
  ],
  "methods": {

  }
}

处理字符串化参数时,应在查询筛选器中以简化的扩展ISO格式将日期值作为字符串发送

下面是一个示例,说明如何使用toISOString方法以非常简单的方式执行此操作:

const dataValue = new Date('10 May 2018 19:30 UTC');
console.log(dataValue.toISOString());
// output: 2018-05-10T19:30:00.000Z

处理字符串化参数时,应在查询筛选器中以简化的扩展ISO格式将日期值作为字符串发送

下面是一个示例,说明如何使用toISOString方法以非常简单的方式执行此操作:

const dataValue = new Date('10 May 2018 19:30 UTC');
console.log(dataValue.toISOString());
// output: 2018-05-10T19:30:00.000Z