I couldn't update object value and figure out how...
The thing I wan to do is if the frequency is one time, return query with 'creatorDetails.uuid' and measureId.
hasCompleted(userId) {
// ...
const query = {
'creatorDetails.uuid': userId,
measureId: this._id,
};
if (this.typeOptions.frequency === Frequency.ONE_TIME) {
return query;
console.log('query - inside', query);
}
// ...
console.log('query - outside', query);
return MeasureData.find(query).count() > 0;
}
But this didn't work well.
console.log('query - inside, query) shows
{
creatorDetails.uuid: "wD7BaeY6SsNSBgjbg"
measureId: "RMLY8unSwSJtRDde2"
}
console.log('query - outside, query) shows
{
creatorDetails.uuid: "wD7BaeY6SsNSBgjbg"
measureId: "PPt4tnGNxW34F5o8t"
}
So I tried below.
hasCompleted(userId) {
// ...
const query = {
'creatorDetails.uuid': userId,
measureId: this._id,
};
if (this.typeOptions.frequency === Frequency.ONE_TIME) {
// query.creatorDetails.uuid = userId; // this cause an error as well
query.measureId = this._id; // added
console.log('query - inside', query);
}
// ...
console.log('query - outside', query);
return MeasureData.find(query).count() > 0;
}
console.log('query - inside', query) shows the same
{
creatorDetails.uuid: "wD7BaeY6SsNSBgjbg"
measureId: "RMLY8unSwSJtRDde2"
}
console.log('query - outside', query) shows also the same
{
creatorDetails.uuid: "wD7BaeY6SsNSBgjbg"
measureId: "PPt4tnGNxW34F5o8t"
}
Somehow measureId gets overwrote by a strange measureId PPt4tnGNxW34F5o8t
I also tried push inside of hasCompleted()
if (this.typeOptions.frequency === Frequency.ONE_TIME) {
query.measureId.push(this._id)
}
But I got an error saying query.measureId.push id not a function
Does anybody know how to update query? Thank you
I think there is a problem with the code:
You have to console first and then return otherwise it won't call the console.log because of unreachable statement
hasCompleted(userId) {
// ...
const query = {
'creatorDetails.uuid': userId,
measureId: this._id,
};
console.log(this.typeOptions.frequency, Frequency.ONE_TIME, this.typeOptions.frequency === Frequency.ONE_TIME);
if (this.typeOptions.frequency === Frequency.ONE_TIME) {
console.log('query - inside', query);
return query; // you have return after doing the console
}
// ...
console.log('query - outside', query);
return MeasureData.find(query).count() > 0;
}
Can you try this and let me know if there is any change in output or not?