2017年11月7日 星期二

How to enable hardware virtualization on a MacBook?

開發環境在 Mac 上的 windows 10 Home
需要啟用 virtualization ,
可是硬體本身有支援,卻沒有啟用 virtualization,
但是該如何啟用呢?
方式如下:

How to enable virtualization in Boot Camp.
  1. holding the option key on startup, boot in to OS X. 
  2. Then go to System Preferences 
  3. Startup Disk and choose your Boot Camp partition
  4. The computer will restart and boot into Windows, with virtualization enabled.

OK! 附張成功的圖紀念一下!

2017年11月1日 星期三

Error: The 'Microsoft.ACE.OLEDB.12.0' provider is not registered on the local machine.

使用 LinqToExcel 突然發生 error,原來是安裝 windows update 後,發生的慘案
Error: The 'Microsoft.ACE.OLEDB.12.0' provider is not registered on the local machine.

解決方案如下:

1. uninstall KB4041676 on Windows 10 and KB4041681 on Windows 7.

2. Find prior version (4.0.9801.0) of msexcl40.dll

3. Place in another directory. They suggest the application directory, but since in the next step you will modify registry to point to this older version, it can probably go anywhere.

4. Update registry key HKEY_LOCAL_MACHINE\SOFTWARE\Wow6432Node\Microsoft\Jet\4.0\Engines\Excel\win32 to point to the location from step 2.

但此招是救急,可能有安全性的疑慮!

2017年10月16日 星期一

query pagination data with mongoose


目前透過 mongoose 查詢 pagination 資料覺得很麻煩,
先取得 total count 再取 page data,
目前使用的方式如下:

    user
        .find(where)
        .count()
        .then((totalCount) => {
            if (totalCount && totalCount > 0) {
                return user.find(where)
                    .select('field1 field2 field3 filed4')
                    .sort({ createdOn: -1 })
                    .skip(skip)
                    .limit(limit)
                    .exec()
                    .then((rows) => {
                        return {
                          rows:rows
                          totalCount: totalCount
                    });
            } else {
                 return res.status(200)
                        .json({
                         rows: [],
                         skip: skip,
                         limit: limit,
                         totalCount: 0
                        })
                        .end();
            }
        })
        .then((result) => {
                 return res.status(200)
                        .json({
                         rows: result.rows,
                         skip: skip,
                         limit: limit,
                         totalCount: result.totalCount
                        })
                        .end();
        })
        .catch((err) => {
            next(err);
        });

寫得落落長,太麻煩了,花了一些時間查查,有沒有什麼方便的方式
原來 mongoose 有 pagination plugin 可以用,真的是佛心來的,
使用方式如下:

Installation:
npm install mongoose-paginate

Usage:
var mongoose         = require('mongoose');
var mongoosePaginate = require('mongoose-paginate');
 
var schema = new mongoose.Schema({ /* schema definition */ });
schema.plugin(mongoosePaginate);
 
var Model = mongoose.model('Model',  schema); // Model.paginate() 

Model.paginate([query], [options], [callback])

所以上面的範例可以改成:
var where   = {};
var options = {
    select:   'field1 field2 field3 filed4',
    sort:     { createdOn: -1 },
    lean:     true,
    offset:   20, 
    limit:    10
};
user.paginate(where, options).then(function(result) {
    return res.status(200)
                        .json({
                         rows: result.docs,
                         skip: skip,
                         limit: limit,
                         totalCount: result.total
                        })
                        .end()
});

太好了,是不是簡單很多!

reference:https://www.npmjs.com/package/mongoose-paginate

Avoiding Callback Hell in Node.js - Promise


寫 nodejs 就會用到非同步的 callback
就會碰到所謂的「callback hell」的囧境
什麼是 callback hell? 如下所示:

doSomeAsyncFunc(function () {
  doSomeAsyncFunc(function () {
    doSomeAsyncFunc(function () {
      doSomeAsyncFunc(function () {
        doSomeAsyncFunc(function () {
          doSomeAsyncFunc(function () {
            doSomeAsyncFunc(function () {
              doSomeAsyncFunc(function () {
                doSomeAsyncFunc(function () {
                  // 我到底在第幾層地獄啊!?
              })
            })
          })
        })
      })
    })
  })
})



這樣光要看懂就很辛苦了,還要怎麼維護程式碼呢?

有幾種解決方式:

  1. async.waterfall
  2. promise
  3. async/await

上一篇介紹了 async.waterfall,現在來看看 Promise 如何使用

1. 先將 doSomeAsyncFunc 包裝成 promise 物件
doSomeAsyncFunc(function () {
  return new Promise(function(resolve, reject) {
    // do something, possibly async
    if (/* everythings fine */) {
      resolve(result);
    } else {
      reject(err);
    }
  });
}

2. 使用 primise.then 即可攤平 callback hell

var promise = doSomeAsyncFunc();
promise.then((result) => {
  // get result
  return '2';
}).then((result2) => {
  // get result2 ('2')
  return '3';
}).then((result3) => {
  // get result3 ('3')
}).catch((err) => {
  // error handling
});

3. 所以一開始的範例就可以改成:

var promise = doSomeAsyncFunc();
promise.then((result) => {
  // do something
  return doSomeAsyncFunc();
}).then((result) => {
  // do something
  return doSomeAsyncFunc();
}).then((result) => {
  // do something
  return doSomeAsyncFunc();
}).then((result) => {
  // do something
  return doSomeAsyncFunc();
}).then((result) => {
  // do something
  return doSomeAsyncFunc();
}).catch((err) => {
  // error handling
});

這樣就可以攤平可怕的 callback hell,回到地球表面囉!

2017年9月26日 星期二

Avoiding Callback Hell in Node.js - async.waterfall


寫 nodejs 就會用到非同步的 callback
就會碰到所謂的「callback hell」的囧境
什麼是 callback hell? 如下所示:

doSomeAsyncFunc(function () {
  doSomeAsyncFunc(function () {
    doSomeAsyncFunc(function () {
      doSomeAsyncFunc(function () {
        doSomeAsyncFunc(function () {
          doSomeAsyncFunc(function () {
            doSomeAsyncFunc(function () {
              doSomeAsyncFunc(function () {
                doSomeAsyncFunc(function () {
                  // 我到底在第幾層地獄啊!?
              })
            })
          })
        })
      })
    })
  })
})


這樣光要看懂就很辛苦了,還要怎麼維護程式碼呢?

有幾種解決方式:

  1. async.waterfall
  2. promise
  3. async/await


先來看看 async.waterfall 如何解決問題

步驟如下:

1. 先安裝 async package :   $ npm install --save async

2. import async package :   var async = require("async");

3. waterfall(tasks, callback) 的說明:

Runs the tasks array of functions in series, each passing their results to the next in the array. However, if any of the tasks pass an error to their own callback, the next function is not executed, and the main callback is immediately called with the error.

簡單來說,只要 error 的參數是 null ,就會一直往下丟 callback,
若中途呼叫 callback 且 error 參數不為 null ,則直接進入 mail callback

範例:
async.waterfall([
    function(callback) {
        callback(null, 'one', 'two');
    },
    function(arg1, arg2, callback) {
        // arg1 now equals 'one' and arg2 now equals 'two'
        callback(null, 'three');
    },
    function(arg1, callback) {
        // arg1 now equals 'three'
        callback(null, 'done');
    }
], function (err, result) {
    // result now equals 'done'
});

也就是說,一開始的 callback hell sample 可以改成:
async.waterfall([
    function(callback) {
        doSomeAsyncFunc1(function (error, result) {
            callback(error, result);
        });
    },
    function(callback) {
        doSomeAsyncFunc2(function (error, result) {
            callback(error, result);
        });
    },
    function(callback) {
        doSomeAsyncFunc3(function (error, result) {
            callback(error, result);
        });
    }
    ......
], function (err, result) {
    // main callback 
    if (err) {
        // do something
    } else if {
        // do something
    }
});

這樣就可以攤平可怕的 callback hell,回到地球表面囉!

reference: https://caolan.github.io/async/docs.html#waterfall

2017年8月9日 星期三

MySQl Error: Data too long for text column


近期站台在塞資料時,
 MySQL 拋出以下錯誤:

Error Message:
Data too long for column 'xxxx' at row 1

發現要塞 80k 資料進 text 型別的欄位失敗了!
看來是跟 MSSQL 的設計不同 ( 我是從 MSSQL 遷徙過來的 )
為了得到正確的觀念,查了一下 MySQL TEXT 欄位的定義
原來 MySQL 還有對 Text 再做細分,如下:

      Type | Maximum length
-----------+-------------------------------------
  TINYTEXT |           255 (2 8−1) bytes
      TEXT |        65,535 (216−1) bytes = 64 KiB
MEDIUMTEXT |    16,777,215 (224−1) bytes = 16 MiB
  LONGTEXT | 4,294,967,295 (232−1) bytes =  4 GiB

以後要用 TEXT 欄位,還是要多注意實際情況選擇正確的型別囉!

2017年8月8日 星期二

讀書心得:如何設計好網站


近期一直困擾著什麼樣的操作方式對 user 才是好的,

之前一直覺得好不好用應該問使用者,
請使用者提出一些意見或想法,
但使用者幾乎不可能提出什麼好的想法,
亨利福特曾說:如果當年去問顧客要什麼,他們肯定會回答我:給我一匹更快的馬

所以找看看有沒有什麼幫助的書,
找到了這本覺得不錯,分享給各位

書名:如何設計好網站:Dont Make Me Think
ISBN:9789863750086


簡單分享一下心得:

  1. 別讓 user 動腦去想
    • 這絕對也是最重要的易用性規則,不用動腦就知道怎麼用,就對了
  2. 使用者看網頁,是用掃視、矇著用的
    • 使用者上網站,大致掃一遍網頁,看到有興趣的就點進去,不會從頭到尾看完的
  3. 省略不必要的贅字
    • 作者建議一段文字,先篩選掉 1/3 後,再篩選掉 1/3,這樣就差不多了
  4. 麵包屑很重要,讓 user 知道人在何處
  5. 每頁都應該有固定的地方,可以回到首頁
    • 如:左上方的 logo,當user迷路時,可以有重新開始的機會
  6. 易用性測試
    1. 場地:二間會議室
    2. 參與人員:受測者,陪測者,觀測者
      1. 受測者,作者建議不要找相關領域,因為是不是相關領域根本不重要,
      2. 專家級的使用者,不會介意多幾個清楚的說明
    3. 週期:一個月一次
      1. 如:每月第四週星期三上午
    4. 什麼時候開始測?
      1. 開發前就可以開始測
      2. 建立簡單的雛型就可以測
      3. 沒有雛型,那就測競爭對手的網站
    5. 情境:
      1. 聘請相關或不相關領域的受測者,進行功能操作,
        會有一名陪測員說明系統及測試的任務,如:建立版型,版型發佈等等,
      2. 陪測員不能刻意引導受測者
      3. 受者測操作時,需大聲說出目前的想法,如:怎麼這麼難用!
      4. 在測試的 pc 上安裝螢幕分享軟體(含使用者的聲音、滑鼠位置等等),
        同步給另一間會議室的主管、RD相關人等觀察
      5. 觀察者在觀察時,同步記綠發現的問題,並將紀錄到的問題排列優先順序( 投票等等 ),
        ps:可能會有異想不到的發現喔!
      6. 處理的問題量及難度,要可以在下次測試前改完,不可太多
  7. 利用易用性測試,擺脫討論不完的爭論
    • 例如:在勾選某類型資料時,要使用下拉選單還是 checkbox list,
    • 這種每個人都有不同的經驗及偏好,這是爭論不完又浪費時間的;


當然還有其他細節我沒有提到,有興趣的話,可以看看這本書囉!