query // how can i find _createdDate not working

Dear all :

how can i Search with " _createdDate"

let today,date,day,month,year,fullday;
let createdDate,all;
$w.onReady(function () {
 //TODO: write your page related code here...
    date = new Date();
     day = date.getDate();
 month = date.getMonth()+1;
 year = date.getFullYear();

fullday = ""+year +"-"+month+"-"+day+"";
console.log(fullday); //2018-5-30

 wixData.query("morerecord")
         .contains("_createdDate", fullday)
          .find()
         .then((results) => {
         all = results.items;
         createdDate = results.items[0]["_createdDate"];
        console.log(all); //
        console.log(createdDate); //undefined
        })
});

if i remove .contains

wixData.query("morerecord")
          .find()
         .then((results) => {
         all = results.items;
         createdDate = results.items[0]["_createdDate"];
        console.log(all); //all data
        console.log(createdDate); // "2018-05-29T09:30:32.539Z"
        })
});

createdDate = “2018-05-29T09:30:32.539Z”

I try to put “2018-05-29T09:30:32.539Z” but no luck

wixData.query("morerecord")
        .contains("_createdDate", "2018-05-29T09:30:32.539Z")
          .find()
         .then((results) => {
         all = results.items;
         createdDate = results.items[0]["_createdDate"];
        console.log(all);
        console.log(createdDate);
        })
});

Can any one help

Br

any one

Hello Ammar,

The problem here is that you are entering a string as a search parameter while the field in the collection is of type Date/Time.

One solution to your problem is to add a afterInsertionHook to your collection that takes the created date and inserts it into another field (ie createdDateString). Then all you have to do is query this new field for the date you want.

Useful links:
AfterInsertHook - for inserting into new field
.slice() - for getting specific characters in a string

Let me know if this helps
Majd

1 Like

Dear Majd

thank for your help
i have items i need to do Query with created Date and count how many items after the Query

I’m trapped here can you give me an example
i do not know how to use data.js

Br
Ammar

I use this but how can i do Query on this

and if i use beforeInsertionHook dose i need to make new Collection ? ?

export function morerecord_afterQuery(item, context) {
 
let all = item;

let cDate = item._createdDate;

console.log(cDate); 

}

Br

Greetings,

Since _createdDate is a default field that is created for every item, there is no need to run a query on that specific field, for example we can do:

wixData.query("videoGallery") //Your collection name here
    .count()                  // Counts how many items in collection
    .then((results)=> {
        console.log(results);        //2
    })

.count() documentation - https://www.wix.com/code/reference/wix-data.WixDataQuery.html#count

Hope this helps,
if you have any more questions please ask! :slight_smile:

Dear Majd

I need to count how many item created in this day

And how many item created in this week and how many item created in this month

Br

If this is the case then you should try the afteInsertHook I mentioned earlier
Here is the documentation for it here .

First, create a hook in your database like so:

Choose the .afterInsertHook option

Next, we have two parameters to work with(item, context), but for not we just want the item parameter as this has the item that is being inserted. We want to get the items _createdDate field value and translate it into a string so that we can query a string with a string value instead of a date/time value that it currently is.

We can change it into this format: mm-dd-yyyy using this piece of code:
date = new Date(‘2013-03-10T02:00:00Z’);
(date.getMonth()+1) + ‘-’ + date.getDate() + ‘-’ + date.getFullYear();
More on that here .

All together it will look something like this:


Now you can properly query the date using a string like you have been doing.
Hope this helps,
Majd

Dear Majd

check plz

the code

import wixData from 'wix-data';


export function morerecord_afterInsert(item, context) {

let id = item._id;
let createdDate = item._createdDate;
let dateAsSt = new Date ('' + createdDate)
let formatdate = (dateAsSt.getMonth()+1) + '-' + dateAsSt.getDate() + '-' + dateAsSt.getFullYear(); 

console.log(id); // f2646ee1-6a49-435d-b98e-af901cce271a 
console.log(item); // { name: 'w', _id: 'f2646ee1-6a49-435d-b98e-af901cce271a', _owner: '0814be5a-bf0e-4088-bc8f-5f74abf8b52f', _createdDate: 2018-06-01T10:44:18.068Z, _updatedDate: 2018-06-01T10:44:18.068Z } 
console.log(createdDate); //2018-06-01T10:44:18.068Z
console.log(dateAsSt); // 2018-06-01T10:44:18.000Z
console.log(formatdate); // 6-1-2018

let toInsert = {
 "_id": id, // Undefined <<<<<<<<<<<
 "newdateAsSt": formatdate
};

wixData.insert("morerecord", toInsert)
.then((resulte) => {
let items = resulte;
console.log(items);
})
.catch((err)=>{

 let errore = err;
console.log(errore);
});

}

the log

f2646ee1-6a49-435d-b98e-af901cce271a
{ name: 'w', _id: 'f2646ee1-6a49-435d-b98e-af901cce271a', _owner: '0814be5a-bf0e-4088-bc8f-5f74abf8b52f', _createdDate: 2018-06-01T10:44:18.068Z, _updatedDate: 2018-06-01T10:44:18.068Z }
2018-06-01T10:44:18.068Z
2018-06-01T10:44:18.000Z
6-1-2018
Hook afterInsert for collection morerecord result ignored! Expected hook result to resolve to an object with an '_id' property, but got [Undefined]

Thanks for your help
Br

Hello again,

First, we should be updating the row instead of inserting again, it has similar syntax:

 let toUpdate = { 
      "_id": id, 
      "newdateAsSt": formatdate 
};  
 
 wixData.update("myCollection", toUpdate) 
   .then( (results) => { let item = results; //see item below 
 }) 
   .catch( (err) => { let errorMsg = err; 
 }); 

Next, we need to figure out why the id is returning undefined. Try making it a global variable outside the .afterInsert() hook.

Best,
Majd

I am looking to create a filter with filter.eq(“_createdDate”, 30 days I don’t know if I am doing this right.

This is the full code I am working with. It’s for a DynamicDataset to show the just the last 30 days at any given moment.


I receive a note saying that .value; is not a parameter of listRepeater but I am lost. Any Advice?

Also is that place where I add days in, is it in Days, Minutes, or Seconds.

import wixData from 'wix-data';
$w.onReady(function () {
 let value = $w("#listRepeater").value;
 let filter = wixData.filter();
    $w("#dynamicDataset").setFilter(
        filter.eq("_createdDate", 30 days)
    );
});//