【问题标题】:Sent a date in mysql with vuejs, nodejs使用 vuejs、nodejs 在 mysql 中发送日期
【发布时间】:2021-07-31 12:44:45
【问题描述】:

首先我使用后端:nodejs 和 express.js,前端:vuejs 和数据库:mysql。我无法将从 localStorage 获得的日期发送到我的 mysql 数据库。当我发送日期时,它显示如下:0000-00-00。我尝试使用 Date 和 vachar 设置我的 dateLocation 字段,但它不起作用,但日期以正确的格式存储在 localStorage 中。我要发送的日期是从日历中检索到的日期,格式为:12-05- 2021 年。我也尝试过使用“to string”将日期转换为字符串,但没有成功。谁能帮我 ?提前致谢。

//part Frontend vuejs
<script>
methods: {
  sendDate() {
    const dateLoc = {
      dateLocation: localStorage.getItem('dateLocation'),
      dateRetour: localStorage.getItem('dateRetour')
    }
    axios.post('http://localhost:3000/api/location', sendDate)
      .then((response) => {
        console.log(response)
      }).catch((error) => {
        console.log({
          error: error
        })
      })
  }
}
</script>

// part Backend nodejs and express js controller location

exports.location = function(req, res) {
    let dateLocation = req.body.dateLocation
    let retourLocation = req.body.retourLocation
    connection.query('INSERT INTO location SET dateLocation = ? , retourLocation = ?, [dateLocation, retourLocation]', (error, results) => {
          if (results) {
            res.status(201).json({
              results
            })
          } else {
            res.status(401).json({
              error: error
            })
          }
        }

【问题讨论】:

  • 可能是查询缺少闭包,就像在格式化时我注意到查询引号没有关闭?
  • 我刚刚用邮递员做了一个测试,我把日期写成英文格式,它可以工作。你知道如何在 javascript 中将我的日期转换为英文格式吗?谢谢@Shujath
  • 您可以在 JS 中格式化您的日期,使用 momentjs 或使用 Date 构造函数将其拆分,查看 article
  • 感谢@Shujath,我尝试了一些可行的方法,它可能不是最好的方法,但它确实有效

标签: javascript mysql node.js vue.js


【解决方案1】:

试试这个,最好不用完整代码。

data() {
    return { 
        dateLoc: {
            dateLocation : '',
            dateRetour : ''
        } 
    }
},
mounted() {
    this.dataLoc.dateLocation = new Date(localStorage.getItem('dateLocation'));
    this.dataLoc.dateRetour = new Date(localStorage.getItem('dateRetour'));
},
methods: {
    sendDate() {
        axios.post('http://localhost:3000/api/location', this.dateLoc)
            .then((response)=>{
                console.log(response)
            }).catch((error)=>{
                console.log({error : error})
            })
    }
}

我建议不要将格式化的日期存储在数据库中,在需要显示时格式化它。看这个Date and Time

【讨论】:

  • 感谢@Bharath 的回复,我能够找出问题出在哪里。问题在于日期格式。我发送了这样的格式:12-02-2021,但它不起作用,但如果我用这种格式发送它:2021-02-12,它可以工作。你能解释一下如何更改我的日期格式吗?谢谢
【解决方案2】:

我找到了一个解决方案,它可能不是最好的,但它确实有效 :)。如果它可以帮助任何人,我将分享我所做的解决方案。感谢大家试图帮助我。 :)

let newDateDepart = localStorage.getItem('dateDepart')
           console.log(aa)
       
           let year =  newDateDepart.substring(6)
           let month = newDateDepart.substring(3,5,6)
           let day = newDateDepart.substring(2,0)
           let dateDepartFinal= year + "-" + month + "-" + day
           console.log(dateDepartFinal)
          // return 2021-08-16

【讨论】:

    猜你喜欢
    • 2016-03-22
    • 1970-01-01
    • 2013-06-04
    • 1970-01-01
    • 2021-08-24
    • 1970-01-01
    • 2013-08-04
    • 2022-01-13
    • 1970-01-01
    相关资源
    最近更新 更多