Day14 vue.js注册验证帐号是否重复ep2

延续昨天
今天的目标是把注册功能完善!
这是昨天的console.log
https://ithelp.ithome.com.tw/upload/images/20210926/20141007Bk3ygLsuEG.png

来试试看 把id(帐号)设成资料库有的资料会发生什麽事
跑出error了
https://ithelp.ithome.com.tw/upload/images/20210926/20141007pXD3HEz241.png

所以我们不用特别下什麽条件判断帐号是否重复 成功post上去就代表没重复反之则有
那接下来的程序码就可以这样写

 async signup(){
        let  result = await axios.post("http://localhost:3000/users",{
          id:this.useraccount,
          password:this.userpassword,
          email:this.useremail,
          username:this.username
         
        })
       
       console.warn(result);
        if(result.status==201){ 
              alert("注册成功")
          sessionStorage.setItem("user-info",JSON.stringify(result.data))
          this.$router.push({name:'Home'})
         
        }
        else{
          alert('帐号不得重复')
        }
        
      }

至於为什麽这个session接的方式跟login不一样
主要是看console.log一个是阵列形式的一个不是

我们通常都希望注册完之後直接就可以登入 所以就把session给接上去 在顺便push到首页 来看看成果吧!
https://ithelp.ithome.com.tw/upload/images/20210926/20141007oqMgl8JJSc.pnghttps://ithelp.ithome.com.tw/upload/images/20210926/20141007qG7at7Fdb4.png

怕大家忘记 为什麽set完可以接到session
在navbar.vue 我们有设一个mounted

   async mounted(){
   
    let user=sessionStorage.getItem('user-info');
    this.username=JSON.parse(user).username
      this.user_id=JSON.parse(user).id
      if(user){
        this.login=false
      }
      
    },

所以照这个只要我们set session的key名字叫做user-info 在navbar里面都会被接到 并且会被使用!
再来清除session 然後在测试看看帐号打一样的会不会成功跳错误
https://ithelp.ithome.com.tw/upload/images/20210926/20141007SydyfYeskA.png

结果发现不会跑到 else 原因是 post 那边就错了 所以不会接者跑

最後将程序码改成try catch
https://ithelp.ithome.com.tw/upload/images/20210926/20141007VeNjdCe33F.png

 async signup(){
       
  try {
          let result = await axios.post("http://localhost:3000/users",{
          id:this.useraccount,
          password:this.userpassword,
          email:this.useremail,
          username:this.username
         
          
        })
            if(result.status==201){ 
            alert("注册成功")
          sessionStorage.setItem("user-info",JSON.stringify(result.data))
          this.$router.push({name:'Home'})
          window.location.reload()
        }
        
        } catch (error) {
           console.warn(error)
           alert('帐号不得重复')
        }
      
        
      
        
      }
    },

接下来有人会想说 密码太短 或是email格式不对我不想让别人注册耶(由於现在没下任何规则 就算是空值也会注册成功)
我们可以这样解决(vuetify的功能)

export default {
    
   data(){
      return{

        showPassword:false,
        useraccount:'',
        userpassword:'',
        username:'',
        useremail:'',
        inputrule:[
          x=>x.length>0 || "不得为空值"
        ],
      }
    },

在data里面定义一个阵列 叫做inputrule 照这个形式 x的长度需大於0
再来去想要的栏位 下:rules=”inputrule”
https://ithelp.ithome.com.tw/upload/images/20210926/20141007fW3Tzi3fPg.png

就会得到这样的画面
https://ithelp.ithome.com.tw/upload/images/20210926/2014100775yhIeDLi1.png

但如果你填入不同的帐号依然会新增资料 这时候该怎麽办呢??
只要在你的html tag 里面下一个ref
https://ithelp.ithome.com.tw/upload/images/20210926/20141007fYfn9boGWX.png

然後在判断这个ref是否validate就可以了(感恩vuetify赞叹vuetify)
https://ithelp.ithome.com.tw/upload/images/20210926/20141007hpldH6Uyna.png

嘟嘟噜这就是结果!
https://ithelp.ithome.com.tw/upload/images/20210926/20141007y3OrxfcQ0T.png

好了搞了2天终於把注册页给搞好了
明天就先从登出开始好了
我们明天见!


<<:  [Day 12] 列表渲染v-for

>>:  【在 iOS 开发路上的大小事-Day14】Firebase 的登入验证服务介绍

资安学习路上-picoCTF 解题(Reverse)1

Reverse 1.Transformation:考unicode 载下来的档案,看起来编码错误 用...

从内建容器到善用资料结构特性

题组回顾与观念统整 在前三天的刷题实战中,我们一起完成了这三个经典的「基本」题: LeetCode...

Day 23 : 案例分享(7.2) 库存与制造 - 供应商直运、制造出货、采购出货(自动化库存调拨)

案例说明及适用场景 销售订单出货时,公司可以决定要走公司仓出库路线、或是由供应商直送(用於代销),或...

【Day 30】递回

最後一天,我想要用递回(Recursion)来结束我们的三十天! 递回的观念,其实就是让一个函式可以...

[Day27] VSCode Plugin - WakaTime

官网连结 VSCode Marketplace 推荐程度:⭐⭐⭐⭐⭐ 有在写工作日志或周志,或是想...