Skip to Content
0
Jan 06 at 06:55 PM

How to implement a query comparing dates/timestamp in CAP Node js

285 Views Last edit Jan 06 at 06:57 PM 3 rev

Hello,

I am trying to run a query that contains 2 conditions in the where part. One of this condition, I am comparing two dates. Unfortunately I couldn't find a way to make it works. I hope you can help me. thanks

Here is my code :

// DATA-MODEL.cds
entity Crypto {
    key ID                 : UUID;
        ExpireIn           : Timestamp;
        Hash : String;
}
// HANDLER 1
let sCreatedDate = new Date(); 
let sId = req.data.ID.
let sExpireIn = new Date().setDate(sCreatedDate.getDate() + 30);
const updateQuery = UPDATE(Crypto, { ID: sId }).with({
           Hash: sHash,   
            ExpireIn : sExpireIn     
        });
const tx = cds.transaction(req);
const OUpdatedData = await tx.run(updateQuery);	

Here I am facing the issue when I am calling this function. The query throw an issue Error: Cannot set parameter at row: 1. Wrong input for TIMESTAMP type

// HANDLER 2
let minDate = new Date(); 
var sHash = req.data.hash
const sQuery = SELECT.from(Requests).where({ Hash: sHash, ExpireIn: { '>=': minDate } });
return Object.assign (sQuery, { then:(r,e) => this.run(sQuery).then(r,e) });