Search code examples
mongodbdatepatchfeathersjs

FeathersJS Date is saved to database as string


I'm using FeathersJS.

I'm trying to patch a document changing a field with a Date object into a MongoDB database, but I get this field saved as a string and not as a Date object. I'm also using the setNow() hook, and I can see that the field specified in the setNow() hook is saved as a Date, but my field is saved as a string.

Does anybody know why this is happening?


Solution

  • The restriction is that MongoDB itself (unlike Mongoose) does not have a schema so all user submitted data in the body or the query will have to be converted into the type that you need to save in or query from the database in a hook. This is documented here. In your case it would be

    app.service('users').hooks({
      before: {
        create(context) {
          context.data.myDate = new Date(context.data.myDate);
    
          return context;
        }
      }
    });