Reputation: 1802
I want to update a field in one of my models with UTC timestamps (createTimeUTC). But I don't want to overwrite/override the 'createdAt' field. I tried importing moment and using moment.utc().format() to update the record on creation. But even though moment seems to be giving the proper UTC format timestamp, the record in database is still getting created as "null".
Here is my model:
module.exports = {
attributes: {
....
createTimeUTC: {
type: 'datetime',
},
},
afterCreate:function(values,next) {
var moment=require('moment');
values.createTimeUTC= moment.utc().format();
console.log("after create: "+ values.createTimeUTC);
next();
};
Here is my create method in the controller:
myControllerMethod: function(req, res) {
var moment=require('moment');
var params = req.params.all();
params.owner = req.session.user.id;
Note.create({owner: params.owner, title: params.title, content: params.content, createTimeUTC: params.createTimeUTC }).exec(function(err, note) {
if(err) {
var msg = 'Unexpected error has occured';
sails.log.error(err);
res.status(400);
return res.send(msg);
}
else {
console.log("CREATED:"+note.createTimeUTC);
res.redirect("/admin/notes/scratchpad");
}
});
},
Can someone please point where am I going wrong?
And also, is there a way to just convert "createdAt" time to UTC in the front-end than having to create new fields in the backend?
Upvotes: 2
Views: 2270
Reputation: 1802
Working on this further, I found that createdAt
and updatedAt
times are already stored as UTC in MySQL by sails (prints in UTC on console although visible as PDT in views). So I just needed to use moment.utc(note.createdAt).format()
in my view to display the time in UTC. As far as the behavior of sails with MySQL database is concerned, this discussion gives some understanding.
Upvotes: 0
Reputation: 1935
Simply changing the values in the afterCreate
handler won't finalize the changes. The values
object isn't a model instance but a simple object. To save the newly created field, you need to make another database call in afterCreate
:
afterCreate: function(values, next) {
var moment = require('moment');
values.createTimeUTC= moment.utc().format();
Note.update({ id: values.id }, values, next);
};
As far as changing the time format in the front-end is concerned, you can do that in your view if it supports date-related filters. If server-side rendering isn't possible, the only alternative would be to show/update the date/time after the page loads, using plain JS.
If you want the formatted date to appear every time you access the model, it might make sense to do the formatting in the model's toJSON
function:
toJSON: function(){
var moment=require('moment');
var obj = {};
obj.field = this.field;//for all other fields
obj.createdAt = moment.utc(this.createdAt).format();
return obj;
}
Place this inside the attributes
section of your model.
NOTE: If you wish to preserve the date/time format while saving to the database, use type string
instead of datetime
in the model, otherwise it will still reflect the date/time string of the configured locale.
Upvotes: 2