Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/node.js/39.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Node.js 在nodejs中,日期输入未正确发送到数据库_Node.js_Mongodb - Fatal编程技术网

Node.js 在nodejs中,日期输入未正确发送到数据库

Node.js 在nodejs中,日期输入未正确发送到数据库,node.js,mongodb,Node.js,Mongodb,我有这样的问题。我正在用express4制作一个web API。在他们的应用程序中,我创建了一个保存数据的函数。这里我提供我的控制器 const express = require('express'); var router = express.Router(); var Reservation = require('../models/reservation'); router.post("/create",function (req,res) { const newRese

我有这样的问题。我正在用express4制作一个web API。在他们的应用程序中,我创建了一个保存数据的函数。这里我提供我的控制器

const express = require('express');
var router = express.Router();

var  Reservation  = require('../models/reservation');

router.post("/create",function (req,res) {
    const newReservation = new Reservation({
        date: req.body.date,
        from: req.body.from,
        to: req.body.to,
        lab: req.body.lab,
        name: req.body.name,
        email: req.body.email,
        role: req.body.role,
        year: req.body.year,
        reason: req.body.reason,
        state: "Not Comfirm"
    });

    Reservation.saveReservation(newReservation, function (err, reservation) {
        if (reservation) {
            res.send(reservation);
        }

        else {
            console.log('Error in User save :' + JSON.stringify(err, undefined, 2));
        }
    });

});

router.get('/notComfirmed', function (req, res) {
    Reservation.findNotComfirmed(function (err, reservations) {
        if(err) throw err;

        if (!reservations){
            res.json({state:false,msg:"No reservations found"});
        }

        if(reservations){
            res.json(reservations);
        }

    })
});


module.exports = router;
这里提供了我的模型

const mongoose = require('mongoose');
const schema = mongoose.Schema;

const reservationSchema = new schema({
    date:{type:Date, required:true},
    from:{type:String},
    to:{type:String, required:true},
    lab:{type:String, required:true},
    name:{type:String, required:true},
    email:{type:String, required:true},
    role:{type:String, required:true},
    year:{type:String},
    reason:{type:String, required:true},
    state:{type:String, required:true},
});

const Reservation = module.exports= mongoose.model("Reservation",reservationSchema );

module.exports.saveReservation = function (newReservation, callback) {
    newReservation.save(callback);
}


module.exports.findBYDateAndTime = function(date, time, lab, callback){
    const query = {date:date,time:time, lab:lab};
    Reservation.findOne(query,callback);
}

module.exports.findNotComfirmed= function (callback) {
    const query = {state: "Not Comfirm"};
    Reservation.find(query, callback);
}
当我和邮递员试过的时候。正在使用时区保存日期。例如,如果我将日期发送为“2018-06-19”,它将保存在数据库中为“2018-06-19 05:30:00.000”。我想删除该时区。我在谷歌和StackOverflow搜索了很多时间。这些例子不足以满足我的要求。有人能帮我解决这个问题吗

谢谢你

也许这会对你有所帮助。这是使用Mongoose的DateOnly库的实现


这是一个mongoose date only模块,它只存储日期而不担心时区。请告诉我您是否解决了问题

如果您发送日期“2018-06-19”,您希望在DB中看到什么?像这样的“2018-06-19 00:00:00.000”?我只想看到数据。比如“2018-06-19”好的,那么它是日期格式的字符串类型,而不是日期类型。或者使用manan解决方案(如果可行)