2022-09-10

TypeError: conn.execute is not a function
    at Strategy._verify (/workspace/practiceNode/passport/myapp/lib/passport.js:39:39)
    at Strategy.authenticate (/workspace/practiceNode/passport/myapp/node_modules/passport-local/lib/strategy.js:90:12)
    at attempt (/workspace/practiceNode/passport/myapp/node_modules/passport/lib/middleware/authenticate.js:369:16)
    at authenticate (/workspace/practiceNode/passport/myapp/node_modules/passport/lib/middleware/authenticate.js:370:7)
    at Layer.handle [as handle_request] (/workspace/practiceNode/passport/myapp/node_modules/express/lib/router/layer.js:95:5)
    at next (/workspace/practiceNode/passport/myapp/node_modules/express/lib/router/route.js:137:13)
    at Route.dispatch (/workspace/practiceNode/passport/myapp/node_modules/express/lib/router/route.js:112:3)
    at Layer.handle [as handle_request] (/workspace/practiceNode/passport/myapp/node_modules/express/lib/router/layer.js:95:5)
    at /workspace/practiceNode/passport/myapp/node_modules/express/lib/router/index.js:281:22
    at Function.process_params (/workspace/practiceNode/passport/myapp/node_modules/express/lib/router/index.js:335:12)
    at next (/workspace/practiceNode/passport/myapp/node_modules/express/lib/router/index.js:275:10)
    at Function.handle (/workspace/practiceNode/passport/myapp/node_modules/express/lib/router/index.js:174:3)
    at router (/workspace/practiceNode/passport/myapp/node_modules/express/lib/router/index.js:47:12)
    at Layer.handle [as handle_request] (/workspace/practiceNode/passport/myapp/node_modules/express/lib/router/layer.js:95:5)
    at trim_prefix (/workspace/practiceNode/passport/myapp/node_modules/express/lib/router/index.js:317:13)
    at /workspace/practiceNode/passport/myapp/node_modules/express/lib/router/index.js:284:7

원인

db 접속 객체는 내가 async/await로 불러왔다.

따라서 모듈로 exports 해 파일에서 사용하더라도 import한 파일은 우리가 원하는 객체가 아니라 프로미스객체이다. 따라서 then 메서드를 이용해 받아오자.

해결

connection.js

// module file name as mysqlCon
module.exports = async () => {
	// get the client
const mysql = require('mysql2/promise');
// get the promise implementation, we will use bluebird
const bluebird = require('bluebird');
// create the connection, specify bluebird as Promise
const connection = await mysql.createConnection(
	{
		host:'localhost', 
		user: 'hwany', 
		password: '1q2w3e4r',
		database: 'test', 
		Promise: bluebird
	});
return connection
// query database
//const [rows, fields] = await connection.execute('SELECT * FROM `table` WHERE `name` = ? AND `age` > ?', ['Morty', 14]);
}

import.js

let conn = ""
	require('./mysql/sqlcon')()
	.then((res) => {
		conn = res
	});