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
1 Answer
Reset to default 6Mongoose 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条)