Dayjs to JS Date for Mongo - node.js

I need to parse a user supplied date in NodeJS. I found answers stating that using built in Date constructor/parse method is not reliable and I should use some library like Moment. I was confused that there are Moment and MomentJS libraries and found that there is a tiny library DaysJS having the same interface. Parsing works fine but I cannot find a way how to get the JS Date object that I need to pass to Mongo. Is there any aother way than extract the unix milliseconds and pass it to Date constructor?
When I pass daysjs instance to Mongo NodeJS driver, it fails:
const day = dayjs('2020-01-02', 'YYYY-MM-DD');
2020-06-07 10:42:33:093 [error]: Request failedThe dollar ($) prefixed field '$L' in 'info.date.$L' is not valid for storage.
This seems to work:
let publishDate = new Date(1000 * dayjs().unix());
Is it the correct way of daysjs with Mongo?

The error you got is because you tried to pass a DayJS object to mongodb, which it doesn't support.
You can pass a javascript Date object to mongodb. In order to convert a DayJS object to plain javascript Date object you can use .toDate() on the DayJS object
dayjs('2020-01-02', 'YYYY-MM-DD').toDate()

Related

how to store date in the firebase with type 'timestamp' instead of 'map' from reactjs

From react application i'm getting date from a datepicker. i'm trying to save it on
firebase. It's saving properly in the database but not TYPE as 'timestamp', instead it's saving like 'map'. I'm sending data to firebase function created by us. from that function we are saving data to collection. any suggestion please.
const d = new Date("27-5-2021")
firebase.firestore.Timestamp.fromDate(d)
(OR)
firebase.firestore.Timestamp.fromMillis(d.getTime())
I think you will need to do something like this to save it as timestamp on firebase.
firebase.firestore.Timestamp.fromDate(date).toDate());

Unable to use Code data type in Mongoose Schema

I wanted to store a data type of Code in my MongoDB database per the docs.
The issue is that when I use Mongoose to specify this type, I get an error:
ReferenceError: Code is not defined
And yes, Mongoose's doc's don't have a type of Code.
Say I want to save Javascript code in my MongoDB database, how could I do so?
For more insight into why I am doing this: I want to save Puppeteer scripts in the database, and using the FS module add them to a file, execute them and send the results as a response object back to the client.
Right now, the code is saved as String, which seems to cause execution errors.
For more information, please don't hesitate to ask.
Thank you,

How to get string and Date object from TIMESTAMP in Firebase Functions?

I got timestamp in Firebase Functions with
admin.database.ServerValue.TIMESTAMP
However, when I pass it as a string, it becomes [object object]
I saved it in Firebase Realtime database and when I checked it from console I saw 1505298866520 which I think it is milliseconds since UNIX epoch.
Do you know how can I get string from it or how can I do time calculations with it? Or what type of object TIMESTAMP returns?
The firebase.database.ServerValue.TIMESTAMP or admin.database.ServerValue.TIMESTAMP hold the following object {.sv: "timestamp"}. This is a specific firebase-database object. That tells the server it should use the server's UNIX-time when an object is added to the database.
That is the reason you can not use it as a date object in javascript.
If you use admin.database.ServerValue.TIMESTAMP on the server via cloud functions it should represent the same as new Date().getTime()

mongo find inside sails native not working

I am trying to query directly on mongo using sails native adapter. I am not getting any result inspite of document present in db. Direct Waterline find functions returns documents. However I want to use native find for some other purpose and trying to make it work. Any suggestions? Output of below : null [].
User.native(function(e,collection){
collection.find({phoneNumber:mPhoneNumber}).toArray(function(e,r){console.log(e,r);});
It worked. Seems orm native does not convert number in string format to integer while querying. Converted mPhoneNumber to Integer using parseInt and it worked.
Here is the updated code
User.native(function(e,collection){ collection.find({phoneNumber:parseInt(mPhoneNumber)}).toArray(function(e,r){console.log(e,r);});

Wrong time format fetched

I am using Node for fetching data from MySQL. In database, i got record like : 2013-08-13 15:44:53 . But when Node fetches from Database , it assigns value like 2013-08-19T07:54:33.000Z.
I just need to get time format as in MySQL table. Btw ( My column format is DateTime in MySQL)
In Node :
connection.query(post, function(error, results, fields) {
userSocket.emit('history :', {
'dataMode': 'history',
msg: results,
});
});
When retrieving it from the database you most likely get a Date object which is exactly what you should work with (strings are only good to display dates, but working on a string representation of a date is nothing you want to do).
If you need a certain string representation, create it based on the data stored in the Date object - or even better, get some library that adds a proper strftime-like method to its prototype.
The best choice for such a library is moment.js which allows you to do this to get the string format you want:
moment('2013-08-19T07:54:33.000Z').format('YYYY-MM-DD hh:mm:ss')
// output (in my case on a system using UTC+2 as its local timezone):
// "2013-08-19 09:54:33"
However, when sending it through a socket (which requires a string representation) it's a good idea to use the default one since you can pass it to the new Date(..) constructor on the client side and get a proper Date object again.

Resources