Nozbe / WatermelonDB

🍉 Reactive & asynchronous database for powerful React and React Native apps ⚡️
https://watermelondb.dev
MIT License
10.62k stars 600 forks source link

TimeStamp #1728

Open MUR-88 opened 10 months ago

MUR-88 commented 10 months ago

async function mySync() { try { await synchronize({ database, pullChanges: async ({ schemaVersion, lastPulledAt, migration }) => { const urlParams = last_pulled_at=${lastPulledAt}&schema_version=${schemaVersion}&migration=${encodeURIComponent( JSON.stringify(migration), )} console.log("URL Params", urlParams); const response = await API.get(master_sector/sync?${urlParams}); console.log(response); // Check if the request was successful if (response.status_code !== 200) { throw new Error(Request failed with status ${response.status}); } const timestamp = dayjs().unix(); console.log("timestamp", timestamp); return { changes: response.data, timestamp: timestamp }; },

  });
} catch (error) {
  console.log("Catch Mysync",error);
}

}

the timestamp value not read properly. the value 01/jan/1970

any idea?

garygcchiu commented 10 months ago

Here's your code reformatted to make it easier for people to follow and help:

async function mySync() {
    try {
        await synchronize({
            database,
            pullChanges: async ({ schemaVersion, lastPulledAt, migration }) => {
                const urlParams = `last_pulled_at=${lastPulledAt}&schema_version=${schemaVersion}&migration=${encodeURIComponent( JSON.stringify(migration))}`
                const response = await API.get(`master_sector/sync?${urlParams}`);

                // Check if the request was successful
                if (response.status_code !== 200) {
                    throw new Error(`Request failed with status ${response.status}`);
                }

                const timestamp = dayjs().unix();
                return { 
                    changes: response.data, 
                    timestamp: timestamp 
                };
            },
        });
    } catch (error) {
        console.log("Catch Mysync",error);
    }
}

Also, since you added console.log() statements to it, would you mind sharing what they output? It would also make it easier for others to follow and help.

kulak91 commented 8 months ago

const timestamp = dayjs().unix(); will always return 1970 jan. You get what you set. If you need current timestamp try to think about dayjs().valueOf()