-
-
Save Anshul0305/aace32e8eabcb2ef49835d0079091e00 to your computer and use it in GitHub Desktop.
const mysql = require('mysql'); | |
function connectToDatabase(){ | |
const connection = mysql.createConnection({ | |
host : 'HOST', | |
user : 'USER', | |
password : 'PASSWORD', | |
database : 'DATABASE' | |
}); | |
return new Promise((resolve,reject) => { | |
connection.connect(); | |
resolve(connection); | |
}); | |
} | |
function queryDatabase(connection){ | |
return new Promise((resolve, reject) => { | |
connection.query('YOUR_SQL_QUERY', (error, results, fields) => { | |
resolve(results); | |
}); | |
}); | |
} | |
function insertIntoDatabase(connection, data){ | |
return new Promise((resolve, reject) => { | |
connection.query('INSERT INTO users SET ?', data, (error, results, fields) => { | |
resolve(results); | |
}); | |
}); | |
} | |
function updateDatabase(connection, data){ | |
return new Promise((resolve, reject) => { | |
connection.query(`UPDATE users SET ? WHERE email = ?`, [data, data.email], (error, results, fields) => { | |
resolve(results); | |
}); | |
}); | |
} | |
function deleteFromDatabase(connection, email){ | |
return new Promise((resolve, reject) => { | |
connection.query(`DELETE from users WHERE email = ?`, email, (error, results, fields) => { | |
resolve(results); | |
}); | |
}); | |
} | |
function handleReadFromMySQL(agent){ | |
const user_email = agent.parameters.email; | |
return connectToDatabase() | |
.then(connection => { | |
return queryDatabase(connection) | |
.then(result => { | |
console.log(result); | |
result.map(user => { | |
if(user_email === user.email){ | |
agent.add(`First Name: ${user.first_name} and Last Name: ${user.last_name}`); | |
} | |
}); | |
connection.end(); | |
}); | |
}); | |
} | |
function handleWriteIntoMysql(agent){ | |
const data = { | |
first_name: "test", | |
last_name: "user", | |
email: "[email protected]" | |
}; | |
return connectToDatabase() | |
.then(connection => { | |
return insertIntoDatabase(connection, data) | |
.then(result => { | |
agent.add(`Data inserted`); | |
connection.end(); | |
}); | |
}); | |
} | |
function handleUpdateMysql(agent){ | |
const data = { | |
first_name: "Anshul", | |
last_name: "Random", | |
email: "[email protected]" | |
}; | |
return connectToDatabase() | |
.then(connection => { | |
return updateDatabase(connection, data) | |
.then(result => { | |
agent.add(`Data updated`); | |
connection.end(); | |
}); | |
}); | |
} | |
function handleDeleteFromMysql(agent){ | |
const email = "[email protected]"; | |
return connectToDatabase() | |
.then(connection => { | |
return deleteFromDatabase(connection, email) | |
.then(result => { | |
agent.add(`Data deleted`); | |
connection.end(); | |
}); | |
}); | |
} | |
let intentMap = new Map(); | |
intentMap.set('getDataFromMySQL', handleReadFromMySQL); | |
intentMap.set('writeDataIntoMysql', handleWriteIntoMysql); | |
intentMap.set('updateMysql', handleUpdateMysql); | |
intentMap.set('deleteFromMysql', handleDeleteFromMysql); | |
agent.handleRequest(intentMap); |
Hello, I did the same as your code, but in the handleReadFromMySQL function it shows the following error:
typeError cannot read properties of undefined (reading 'map').
My node version is v16.14.2.
Could you help me, please?
Hey there! thanks a lot, it was working for me few times and now I´m not able to make it work again.
TypeError: Cannot read property 'map' of undefined
at queryDatabase.then.result (/workspace/index.js:46:16)
at process._tickCallback (internal/process/next_tick.js:68:7)
Index.js Line 46
result.map(user => {
After that, the flow crash
Function execution took 10375 ms. Finished with status: crash
It is not credentials, network o long time response, any idea?
Package.json
"dependencies": {
"actions-on-google": "^2.2.0",
"firebase-admin": "^5.13.1",
"firebase-functions": "^2.0.2",
"dialogflow": "^0.6.0",
"dialogflow-fulfillment": "^0.5.0",
"mysql": "^2.17.1"
}
Great tutorials, I ready to buy the udemy course
Muy interesante, me gustaria ver el proyecto completo