Calculated field base on another field. How to update?


#1

Hello this code updates a record in a collection

Task.update(id, {‘status’:‘doing’});

however i need to calculate a field based on another field of the same record.
how di i do it ?
do i have to find the record first and then update it ?


#2

try these


#3

Yes you do. But if you can infer the second field based on the first one, are you sure you actually need to store the second field? Or can’t you store the first and second fields at the same time?


#4

This is the case
I have a record with creation date and another field named “text” that stores the elapsed time from the creation to now.

I then have a timer that each minute updates this elapsed time but needs to get first the creation time

I solved it using this code as suggested by @corvid

list.before.update(function (userId, doc, fieldNames, modifier, options) {
modifier.$set = modifier.$set || {};
m1=moment(doc.data);
text=moment.preciseDiff(m1,Date.now());
modifier.$set.text=text;

});

Maybe there is a better way but for now i´m good.