Suthura Sudharaka
Suthura Sudharaka

Reputation: 673

gives same output every time for every request

I have made an API using node JS and mongo db. There I have created a route to take result set with data parameters

This is the route I have created

router.post('/getmyweeksales', async(req, res) => {

    function remDays(date, days) {
        var result = new Date(date);
        result.setDate(result.getDate() - days);
        console.log(result);

        return result;
    }

    const verified = jwt.verify(req.body.token, process.env.TOKEN_SECRET);
    const date = req.body.currentdate;


    const sales = await Sale.find({
        userID: verified._id,
        saletime: {
            "$gte": remDays(date, 7).toISOString(),"$lt": Date(date)
        }
    }).sort({ saletime: -1 });

    res.send(sales);
});

After adding those date parameters it gives the same out put to every request I send.

If I remove this line it gives me every result without any error

saletime: {
                "$gte": remDays(date, 7).toISOString(),"$lt": Date(date)
            }

The following is what I receive everytime.

[
    {
        "_id": "5e9b2b320b85d100178a3233",
        "userID": "5e931e4661c5c000170bcdc5",
        "saledata": [
            {
                "_id": "5e9b2b320b85d100178a3235",
                "itemName": "sample item 1",
                "quantity": "1",
                "itemTotal": "123.0"
            },
            {
                "_id": "5e9b2b320b85d100178a3234",
                "itemName": "sample item 3",
                "quantity": "1",
                "itemTotal": "123.0"
            }
        ],
        "total": "246",
        "saletime": "2020-04-18T22:00:40.044Z",
        "__v": 0
    }
]

How can I receive all the results withing 7 days ?

Upvotes: 0

Views: 58

Answers (1)

Suthura Sudharaka
Suthura Sudharaka

Reputation: 673

After some research I found that the problem was with Date(date) it cast the date to another format removing that Date and using only "$lt": date solved the problem. Then had to send the data in the same format as mongodb stores it

Upvotes: 2

Related Questions