text: "use strict";(self.webpackChunkvuepress_blog=self.webpackChunkvuepress_blog||[]).push([[30554],{7816:(n,s,a)=>{a.r(s),a.d(s,{data:()=>e});const e={key:"v-d18cb476",path:"/typora/01frontEnd/16nodeFramework/024Sequelize%E6%B3%A8%E5%86%8C%E7%99%BB%E5%BD%95.html",title:"024 Sequelize注册登录",lang:"zh-CN",frontmatter:{},excerpt:"",headers:[],filePathRelative:"typora/01frontEnd/16nodeFramework/024Sequelize注册登录.md",git:{updatedTime:1631527134e3}}},8701:(n,s,a)=>{a.r(s),a.d(s,{default:()=>p});const e=(0,a(36425).uE)('<h1 id="_024-sequelize注册登录" tabindex="-1"><a class="header-anchor" href="#_024-sequelize注册登录" aria-hidden="true">#</a> 024 Sequelize注册登录</h1><p>导入模型对象，模型对象.方法名即可操作数据库增删改查。</p><div class="language-javascript ext-js line-numbers-mode"><pre class="language-javascript"><code><span class="token comment">/* Sequelize处理注册登录\n\n */</span>\n<span class="token keyword">const</span> User <span class="token operator">=</span> <span class="token function">require</span><span class="token punctuation">(</span><span class="token string">&#39;../db/model/user&#39;</span><span class="token punctuation">)</span>\n<span class="token keyword">async</span> <span class="token keyword">function</span> <span class="token function">getUser</span><span class="token punctuation">(</span><span class="token parameter">username<span class="token punctuation">,</span> password</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>\n    <span class="token comment">// username = escape(username)</span>\n    <span class="token comment">// password = escape(password)</span>\n    <span class="token keyword">if</span> <span class="token punctuation">(</span>password<span class="token punctuation">)</span><span class="token punctuation">{</span>\n        <span class="token comment">// let result = await User.findOne({</span>\n        <span class="token keyword">let</span> result <span class="token operator">=</span> <span class="token keyword">await</span> User<span class="token punctuation">.</span><span class="token function">findAll</span><span class="token punctuation">(</span><span class="token punctuation">{</span>\n            where<span class="token operator">:</span> <span class="token punctuation">{</span>\n                username<span class="token operator">:</span> username<span class="token punctuation">,</span>\n                password<span class="token operator">:</span> password\n            <span class="token punctuation">}</span>\n        <span class="token punctuation">}</span><span class="token punctuation">)</span>\n        <span class="token keyword">return</span> result\n        <span class="token comment">// let sql = `select * from user where username = ${username} and password = ${password}`</span>\n        <span class="token comment">// let result = await exc(sql)</span>\n        <span class="token comment">// return result</span>\n    <span class="token punctuation">}</span><span class="token keyword">else</span> <span class="token punctuation">{</span>\n        <span class="token keyword">let</span> result <span class="token operator">=</span> <span class="token keyword">await</span> User<span class="token punctuation">.</span><span class="token function">findAll</span><span class="token punctuation">(</span><span class="token punctuation">{</span>\n            where<span class="token operator">:</span> <span class="token punctuation">{</span>\n                username<span class="token operator">:</span> username\n            <span class="token punctuation">}</span>\n        <span class="token punctuation">}</span><span class="token punctuation">)</span>\n        <span class="token keyword">return</span> result\n        <span class="token comment">// let sql = `select * from user where username = ${username}`</span>\n        <span class="token comment">// let result = await exc(sql)</span>\n        <span class="token comment">// return result</span>\n    <span class="token punctuation">}</span>\n<span class="token punctuation">}</span>\n\n<span class="token keyword">async</span> <span class="token keyword">function</span> <span class="token function">createUser</span><span class="token punctuation">(</span><span class="token parameter"><span class="token punctuation">{</span>username<span class="token punctuation">,</span> password<span class="token punctuation">,</span> sex<span class="token punctuation">}</span></span><span class="token punctuation">)</span><span class="token punctuation">{</span>\n    <span class="token comment">// let sql = `insert into user (username, password, sex) values(&#39;${username}&#39;,&#39;${password}&#39;,&#39;${sex}&#39;)`</span>\n    <span class="token comment">// let result = await exc(sql)</span>\n    <span class="token comment">// return result</span>\n    <span class="token keyword">let</span> result <span class="token operator">=</span> <span class="token keyword">await</span> User<span class="token punctuation">.</span><span class="token function">create</span><span class="token punctuation">(</span><span class="token punctuation">{</span>\n        username<span class="token operator">:</span> username<span class="token punctuation">,</span>\n        password<span class="token operator">:</span> password<span class="token punctuation">,</span>\n        sex<span class="token operator">:</span> sex\n    <span class="token punctuation">}</span><span class="token punctuation">)</span>\n    <span class="token keyword">return</span>  result<span class="token punctuation">[</span><span class="token string">&#39;dataValues&#39;</span><span class="token punctuation">]</span>\n<span class="token punctuation">}</span>\nmodule<span class="token punctuation">.</span>exports <span class="token operator">=</span> <span class="token punctuation">{</span>\n    getUser<span class="token punctuation">,</span>\n    createUser\n<span class="token punctuation">}</span>\n</code></pre><div class="line-numbers"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br><span class="line-number">14</span><br><span class="line-number">15</span><br><span class="line-number">16</span><br><span class="line-number">17</span><br><span class="line-number">18</span><br><span class="line-number">19</span><br><span class="line-number">20</span><br><span class="line-number">21</span><br><span class="line-number">22</span><br><span class="line-number">23</span><br><span class="line-number">24</span><br><span class="line-number">25</span><br><span class="line-number">26</span><br><span class="line-number">27</span><br><span class="line-number">28</span><br><span class="line-number">29</span><br><span class="line-number">30</span><br><span class="line-number">31</span><br><span class="line-number">32</span><br><span class="line-number">33</span><br><span class="line-number">34</span><br><span class="line-number">35</span><br><span class="line-number">36</span><br><span class="line-number">37</span><br><span class="line-number">38</span><br><span class="line-number">39</span><br><span class="line-number">40</span><br><span class="line-number">41</span><br><span class="line-number">42</span><br><span class="line-number">43</span><br><span class="line-number">44</span><br><span class="line-number">45</span><br><span class="line-number">46</span><br><span class="line-number">47</span><br></div></div>',3),p={render:function(n,s){return e}}}}]);