javascript - Save Unix Epoch Timestamp to Mongoose Schema type Date using NodeJS - Stack Overflow

I receive responses from the Stripe API GET invoices endpoint that returns dates as unix timestamps. A

I receive responses from the Stripe API GET /invoices endpoint that returns dates as unix timestamps. An example value is 1573917475. I need to save this value in an ISO Format in Mongoose. Example: 2019-11-16T15:17:55 I'm familiar with how to convert this value into a ISO / UTC formatted date time value using Javascript or MomentJS. However, I would like to set this behavior in the Mongoose Schema if possible.

API response containing timestamp values:

{
    "period_end": 1576509475,
    "period_start": 1573917475
}

Mongoose Schema:

new Schema({
 ... redacted ...
    period_end: { type: Date },
    period_start: { type: Date },
 ... redacted ...
});

This is currently saving the as dates in Mongo with values such as:

{
    "period_end": "1970-01-19T04:34:23.671+0000" 
}

When the year is 1970 this is usually because an issue with the input date format. Can this type of conversion be performed at the Schema level ?

I saw this Mongoose documentation .html that mentions converting the values before saving to the schema. But I would prefer not to loop thru the values manually as I'm saving the raw response from the API.

Edit: Using the answer provided by @ambianBeing I came up with the following solution.

new Schema({
 ... redacted ...
    period_end: { type: Date, set: d => convertSecsToMs(d) },
    period_start: { type: Date, set: d => convertSecsToMs(d) },
 ... redacted ...
});

function convertSecsToMs(d) {
  if (!d || !isValidTimestamp(d)) return;

  return new Date(d * 1000);
}

function isValidTimestamp(date) {
  return new Date(date).getTime() > 0;
}

I receive responses from the Stripe API GET /invoices endpoint that returns dates as unix timestamps. An example value is 1573917475. I need to save this value in an ISO Format in Mongoose. Example: 2019-11-16T15:17:55 I'm familiar with how to convert this value into a ISO / UTC formatted date time value using Javascript or MomentJS. However, I would like to set this behavior in the Mongoose Schema if possible.

API response containing timestamp values:

{
    "period_end": 1576509475,
    "period_start": 1573917475
}

Mongoose Schema:

new Schema({
 ... redacted ...
    period_end: { type: Date },
    period_start: { type: Date },
 ... redacted ...
});

This is currently saving the as dates in Mongo with values such as:

{
    "period_end": "1970-01-19T04:34:23.671+0000" 
}

When the year is 1970 this is usually because an issue with the input date format. Can this type of conversion be performed at the Schema level ?

I saw this Mongoose documentation https://mongoosejs./docs/tutorials/dates.html that mentions converting the values before saving to the schema. But I would prefer not to loop thru the values manually as I'm saving the raw response from the API.

Edit: Using the answer provided by @ambianBeing I came up with the following solution.

new Schema({
 ... redacted ...
    period_end: { type: Date, set: d => convertSecsToMs(d) },
    period_start: { type: Date, set: d => convertSecsToMs(d) },
 ... redacted ...
});

function convertSecsToMs(d) {
  if (!d || !isValidTimestamp(d)) return;

  return new Date(d * 1000);
}

function isValidTimestamp(date) {
  return new Date(date).getTime() > 0;
}
Share Improve this question edited Dec 17, 2019 at 18:48 Andrew Taylor asked Dec 17, 2019 at 17:46 Andrew TaylorAndrew Taylor 6287 silver badges29 bronze badges 2
  • 2 UNIX timestamps are in seconds; JavaScript works in milliseconds. You have to multiply that timestamp value by 1000. – Pointy Commented Dec 17, 2019 at 17:48
  • Yes, I can do the conversion manually. My question was more along the lines of, can this be done in the schema ? – Andrew Taylor Commented Dec 17, 2019 at 17:50
Add a ment  | 

1 Answer 1

Reset to default 6

Mongoose supports setters/getters at the schema level which works with update ops.

const docSchema = new Schema({
  period_start: {
    type: Date,
    set: d => new Date(d * 1000)
  },
  period_end: {
    type: Date,
    set: d => new Date(d * 1000)
  }
});

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745004870a4605718.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信