0

I´m using the API explorer of loopback to create a model with the following parameters:

{
  "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
}

However, The server is always returning a 500 invalid Date error:

{
  "error": {
    "name": "Error",
    "status": 500,
    "message": "Invalid date: Invalid Date",
    "stack": "Error: Invalid date: Invalid Date\n    at DateType }
}

This is my model for reference. It inherits the User model of Loopback.

{
  "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": {

  }
}

Can anyone tell me what´s the problem with the date? I think it is correctly formated because it´s the default parameters that the explorer generates.

Pablo Estrada
  • 3,182
  • 4
  • 30
  • 74

3 Answers3

2

Don't know if you are interested in the answer, but I recently started working with loopback and got the same error as you, the date format that was accepted by the server is like this 2017-01-06T23:58:10.000Z

Hope it helps someone.

Ps.: if the date is not required by the model, don't even send it, not even with "null" or "" value, it will throw a 500 status error.

Paolo Carrara
  • 394
  • 4
  • 13
0

The correct date format is : 2017-10-12T10:31:37.925Z

And If you want to add fields of dateCreated and dateUpdate, Then use date mixin loopback-ds-timestamp-mixin

Install mixin with

npm i loopback-ds-timestamp-mixin --save

Add the mixins property to your server/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"
 ]}
}

And in your model:

{
  "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": {

  }
}
0

When dealing with stringified params you should send the date value as a string in simplified extended ISO format in your query filter.

Here is an example of how you can do this in a very simple way using toISOString method:

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