Hey folks
Context
I am coming today for something which I think might be a small bug, but I let you confirm so
I have a collection of models (mongodb) that are items with X, Y, Z fields, etc (doesn’t matter). Two of them tho are relatively created_at
and updated_at
.These two fields are int64 but for display reason, I want to converter them, however, it doesn’t work as expected:
Situation
I have both of my fields created_at
and updated_at
set with the value of 1611245643
. Once converted, this value should return 21 Jan 2021 17:14:3
(and based on “logs”, this method does work and return the expected result).
However, the following is appearing:
Code
In terms of code, I have the following:
utils/converters.js
function fromTimestampToDate_RFC3339(UNIX_timestamp) {
let a = new Date(UNIX_timestamp * 1000);
let months = ['Jan','Feb','Mar','Apr','May','Jun','Jul','Aug','Sep','Oct','Nov','Dec'];
let year = a.getFullYear();
let month = months[a.getMonth()];
let date = a.getDate();
let hour = a.getHours();
let min = a.getMinutes();
let sec = a.getSeconds();
let time = date + ' ' + month + ' ' + year + ' ' + hour + ':' + min + ':' + sec;
console.log("<"+UNIX_timestamp+"><"+time+">")
return time
}
module.exports = {
fromTimestampToDate_RFC3339: fromTimestampToDate_RFC3339,
}
models/items.js
// This model was generated by Lumber. However, you remain in control of your models.
// Learn how here: https://docs.forestadmin.com/documentation/v/v6/reference-guide/models/enrich-your-models
const mongoose = require('mongoose');
// This section contains the properties of your model, mapped to your collection's properties.
// Learn more here: https://docs.forestadmin.com/documentation/v/v6/reference-guide/models/enrich-your-models#declaring-a-new-field-in-a-model
const schema = mongoose.Schema({
// ...
'created_at': Number,
// ...
'updated_at': Number,
// ...
}, {
timestamps: false,
});
module.exports = mongoose.model('items', schema, 'Items');
forest/items.js
const {collection} = require('forest-express-mongoose');
const {fromTimestampToDate_RFC3339} = require('../utils/converters');
// This file allows you to add to your Forest UI:
// - Smart actions: https://docs.forestadmin.com/documentation/reference-guide/actions/create-and-manage-smart-actions
// - Smart fields: https://docs.forestadmin.com/documentation/reference-guide/fields/create-and-manage-smart-fields
// - Smart relationships: https://docs.forestadmin.com/documentation/reference-guide/relationships/create-a-smart-relationship
// - Smart segments: https://docs.forestadmin.com/documentation/reference-guide/segments/smart-segments
collection('items', {
actions: [{
// ...
}, {
// ...
}],
fields: [{
// ...
} {
// ...
}, {
field: 'created_at',
type: 'String',
get: (item) => {
return fromTimestampToDate_RFC3339(item.created_at);
}
}, {
field: 'updated_at',
type: 'String',
get: (item) => {
return fromTimestampToDate_RFC3339(item.updated_at);
}
}],
segments: [],
});
Any idea? Thanks!
Max