Posts

Showing posts from March, 2021

MySQL 初始化小抄(使用環境:node.js)

安裝 MySQL 和 Workbrench 在 Workbrench 建立 DATABASE CREATE DATABASE 資料庫名稱; 安裝套件 npm install mysql2 sequelize sequelize-cli 執行初始化腳本 npx sequelize init 初始化腳本會建立四個東西 config/config.json 修改資料庫名稱及登入密碼。 (註:operatorsAliases 在 Sequelize v5 後已棄用) models/index.js migrations seeders 設定 model 和 seed: 官方文件

SQL ZOO 試答:SELECT names

SELECT names Find the country that start with Y SELECT name FROM world WHERE name LIKE 'Y%' Find the countries that end with y SELECT name FROM world WHERE name LIKE '%y' Find the countries that contain the letter x SELECT name FROM world WHERE name LIKE '%x%' Find the countries that end with land SELECT name FROM world WHERE name LIKE '%land' Find the countries that start with C and end with ia SELECT name FROM world WHERE name LIKE 'C%ia' Find the country that has oo in the name SELECT name FROM world WHERE name LIKE '%oo%' Find the countries that have three or more a in the name SELECT name FROM world WHERE name LIKE '%a%a%a%' Find the countries that have "t" as the se

「家庭記帳本」專案心得

專案介紹 功能: 使用者能註冊、登入、登出。 登入後,可以新增、刪除、修改、查詢自己的支出紀錄。 可以紀錄支出的用途、金額、日期,並且為支出分類。 可以用分類和月份,篩選要顯示的支出紀錄。 Heroku 網址: https://still-oasis-68074.herokuapp.com/ 程式碼: https://github.com/Flora2020/expense-tracker 你為何會選擇這個專案? 自己本身就有在記帳,管理收支狀況。因為想對這些紀錄有比較高的掌控權,所以我是自己用 LibreOffice 設計表格,而不是使用現成的記帳軟體。但使用起來還是很不方便,例如: 我每個月新增一個 LibreOffice 表格,然後把不同帳戶的收支狀況(例如各個銀行的帳戶、悠遊卡、現金、借貸等等)都獨立成一張表格分頁,分頁之間設定連動關係,以便紀錄跨帳戶的情況,例如從郵局領錢放到錢包裡。但光是為了設定連動關係,表格就變得非常複雜,日後如果增加帳戶,又要額外設定非常多欄位。 果然還是自己設計記帳軟體,比較一勞永逸。不過目前只有紀錄支出,沒有紀錄收入,也沒有紀錄支出來自哪個帳戶,需要增加的功能還很多。 你使用了什麼技術? 在 Node.js 環境下,使用 Express 框架和 Handlebars 的模板引擎。CSS 套用了 Bootstrap 的框架。 資料庫則是用 Mongoose 操作 MongoDB。 用 passport 套件實作使用者驗證。 哪部分你相對能掌握?哪裡花了最多時間? 較能掌握: 沒有使用 passport 套件的地方,我大致明白收到瀏覽器的請求後,請求在伺服器中,按照什麼順序通過哪些 middleware。 花最多時間: 前端切版,以及調整 RWD。對 CSS 語法規則仍然不熟練,有時候寫了指令,卻沒有效果,而且完全不明白為什麼沒有效果。對於 Bootstrap 的框架到底設定了哪些東西,也了解得不夠

用 Math.random 隨機產生整數時,為什麼通常是用 Math.floor 而不是 Math.ceil

Math.random() 的範圍:0 ≤ Math.random() < 1 如果要隨機產生 0~9 這十個數字 使用 Math.floor: 0 ≤ Math.random() * 10 < 10 0 ≤  Math.floor(Math.random()) * 10 ≤ 9 使用 Math.ceil: 0 ≤ Math.random() * 9 < 9 0 ≤ Math.ceil(Math.random()) * 9 ≤ 9 但是 Math.random() 產生 0 的機率非常非常非常非常小,所以 「Math.ceil(Math.random()) * 9」骰出 0 的機率非常非常非常非常小。 相關討論: Random Number, Math.floor(…) vs Math.ceil(…) - stack overflow

[javascript] 使用 array.forEach 時若刪掉陣列元素,有些元素會被跳過去

測試用程式碼  const array = [0,1,2,3] array.forEach((item, index) => {   console.log('item:', item)   array.splice(index, 1) }) console.log(array) 印出了 item: 0 item: 2 [ 1, 3 ]

array = array.splice 以後,array 的值是什麼

根據 Array.prototype.splice() - MDN ,array.splice(0, 1) 會從  array 中刪掉第 0 個元素,然後回傳 [被刪掉的元素]。那麼,執行完 array =  array.splice 這行程式碼, array 會變成 被刪掉第 0 個元素 ,還是 [ 被刪掉的元素 ] 呢? 實驗1: let test = [0, 1] test = test.splice(0, 1) console.log(test)        //印出來是[0] 實驗2: const test = [0, 1] test = test.splice(0, 1) console.log(test) 結果是 TypeError: Assignment to constant variable. 結論:test = test.splice(0, 1) 是把 test 賦值為 [被刪掉的元素]。這個結果非常正常。

專案初始化小抄(工具:node.js + express + handlebars)

用終端機移動到專案資料夾後,用下列指令產生 package.json 檔 npm init -y 修改 package.json 檔,設定入口檔案及腳本,例如: "main": "app.js", "scripts": {     "start": "node app.js",     "dev": "nodemon app.js",     "test": "echo \"Error: no test specified\" && exit 1"   }, 安裝套件,例如 npm install express express-handlebars body-parser method-override 實用套件: body-parser:解析 POST request 的 request body method-override:瀏覽器只有 GET 和 POST 兩種請求方法。為了達成 RESTful API 的設計風格或其他理由,用 method-override 將請求方法改成別的名字,例如 PUT、DELETE 等等 express-session:為 http 請求加入狀態 passport:使用者驗證。不同的驗證方式,需使用對應的驗證策略 passport-local:在本地端進行驗證的驗證策略 bcryptjs:把機密資料(例如使用者的密碼)加鹽並雜湊 faker:產生假資料 multer:上傳檔案 helmet:設定與資訊安全有關的 HTTP 標頭 sanitize-html:消毒 HTML 建立入口檔案,打開該檔案做基本設定: const express = require('express') const exphbs = require('express-handlebars') const bodyParser = require('b

安裝完 MySQL Workbench 卻打不開的解決方式

我的作業系統:macOS Big Sur v11.2.1 MySQL Workbench 版本:macOS (x86, 64-bit), DMG Archive 8.0.23 參考資料: MySQL Workbench cannot open on Mac - stackoverflow 依照回答者 Phil Nguyen 的建議,我在終端機輸入 /Applications/MySQLWorkbench.app/Contents/MacOS/MySQLWorkbench --verbose 得到跟回答者 EdM 一樣的結果 Fatal Python error: initfsencoding: unable to load the file system codec, sys.path = ['/Applications/MySQLWorkbench.app/Contents/Resources/libraries', '/Library/Frameworks/Python.framework/Versions/3.7/lib/python37.zip', '/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7', '/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/lib-dynload'] ModuleNotFoundError: No module named 'encodings' 所以問題大概是出在我沒有安裝 python 然後按照回答者 yakob abada 建議的第一步,在終端機執行 brew install python@3.7 得到 Error: python@3.7: the bottle needs the Apple Command Line Tools to be installed.   You can install them, if desired, with:     xcode-select --install (遵命,電腦叫我安裝什麼,我就安裝什麼)所以我先執行了 xcode-select -