hash
hash

Reputation: 5416

Time value not storing in the MongoDB Database

I didnt get any errors.all values are stored in to database except Time vales.

in here i get the user inputs

<div>
    <input type="time" name="start_time" style="width:200px" placeholder="Strat Time"/><br/>
    <input type="time" name="end_time" style="width:200px" placeholder="End Time"/><br/>
    <input type="text" name="service" style="width:200px" placeholder="Service"/><br/>
    <input type="text" name="client_name" style="width:200px" placeholder="Name"/><br/>
    <input type="text" name="client_phone" style="width:200px" placeholder="Phone"/><br/>
    <input type="text" name="amount" style="width:200px" placeholder="Amount"/><br/>
    <button>Add</button>
</div>

this is my model

var appoinmentSchema = mongoose.Schema({ start_time: Date, end_time : Date, service: String,    client_phone : String, client_name: String, amount : String })
exports.schema = mongoose.model('appoinment', appoinmentSchema);
exports.name = 'appoinment';

this is my server side function

function json_homepage() {

  var self = this;
  var Appoinment = MODEL('appoinment').schema;
  var model = self.body;
  var data = self.post;

  var appoinment = new  Appoinment({ start_time: new Date(model.start_time), end_time: new Date(model.end_time), service: model.service,client_name: model.client_name , client_phone: model.client_phone , amount:model.amount }).save(function(err) {
    console.log(data.start_time);
    console.log(model.start_time);
    if (err)
        self.throw500(err);

    // Read all users
    Appoinment.find(self.callback());
  });

}

this data store in MongoDB like this

  "start_time": {
    "$date": "1970-01-01T00:00:00.000Z"
  },
  "end_time": {
    "$date": "1970-01-01T00:00:00.000Z"
   },

-- update --

RangeError: Invalid time value ([object Object]) RangeError: Invalid time value
  at Date.toISOString (native)
  at Object.json_homepage (D:\Projects\examples-master\mongoose\controllers\default.js:28:68)
  at Subscribe.doExecute (D:\Projects\examples-master\mongoose\node_modules\total.js\index.js:4:4221)
  at Subscribe.execute (D:\Projects\examples-master\mongoose\node_modules\total.js\index.js:4:2949)
  at Subscribe.prepare (D:\Projects\examples-master\mongoose\node_modules\total.js\index.js:4:3911)
  at Subscribe.doEnd (D:\Projects\examples-master\mongoose\node_modules\total.js\index.js:4:6952)
  at IncomingMessage.<anonymous> (D:\Projects\examples-master\mongoose\node_modules\total.js\index.js:4:1808)
  at IncomingMessage.emit (events.js:92:17)
  at _stream_readable.js:943:16
  at process._tickCallback (node.js:419:13)

Any help please.i didnt see any wrong here. help me

Upvotes: 0

Views: 1915

Answers (1)

majidarif
majidarif

Reputation: 20095

First of all. I believe this is invalid:

new Date(model.start_time)

Because this:

<input type="time" name="start_time"/>

Returns a string.

One way you can fix this is by doing something like:

var dateToday = new Date().toLocaleDateString('en-US');
console.log(new Date(dateToday + '' + time));

Solution

var dateToday = new Date().toLocaleDateString('en-US');

var appoinment = new Appoinment({ 
  start_time   : new Date(dateToday + '' + model.start_time), 
  end_time     : new Date(dateToday + '' + model.end_time), 
  service      : model.service,
  client_name  : model.client_name , 
  client_phone : model.client_phone , 
  amount       : model.amount 
}).save(function(err) {

Or you can try this:

start_time : new Date(dateToday + '' + model.start_time).toISOString(),
end_time   : new Date(dateToday + '' + model.end_time).toISOString(), 

You can try it with this jsfiddle or here below.

var calc = document.getElementById("calc")

calc.addEventListener("click", function() {
  var time = document.getElementById("start_time").value
  var date = new Date().toLocaleDateString("en-US");
  var result = new Date(date + " " + time);
  alert(result);
  alert(result.toISOString());
})
Time:
<input id="start_time" type="time" name="start_time" />
<button id="calc">Get Time</button>

Good luck.

Upvotes: 1

Related Questions