时间值不存储在MongoDB数据库中

时间:2014-10-15 11:34:52

标签: node.js mongodb

我没有得到任何错误。除了Time vales之外,所有值都存储在数据库中。

在这里我得到了用户输入

<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>

这是我的模特

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';

这是我的服务器端功能

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());
  });

}

像MongoDB这样的数据存储

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

- 更新 -

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)

任何帮助please.i没有看到任何错误。帮助我

1 个答案:

答案 0 :(得分:1)

首先。我认为这是无效的:

new Date(model.start_time)

因为:

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

返回一个字符串。

解决此问题的一种方法是执行以下操作:

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

解决方案

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) {

或者你可以试试这个:

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

您可以使用此jsfiddle或以下内容进行试用。

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>

祝你好运。