[英]How to read data form cloud SQL in Google Cloud Function in Node JS
I am new in Google Cloud Function, Here am trying to load data from cloud SQL for that I have written a code, which has database connection as well as SQL query string however when am trying to test the function it is throwing either error or Object or sometime it says you are not returning any data.我是 Google Cloud Function 的新手,我在这里尝试从云 SQL 加载数据,因为我编写了一个代码,它具有数据库连接以及 SQL 查询字符串但是当我尝试测试 function 时,它抛出错误或 Object或者有时它说您没有返回任何数据。 I tried many ways to send data but still doesn't work for me.
我尝试了很多发送数据的方法,但仍然对我不起作用。 Thanks in advance.
提前致谢。
Code snippets,代码片段,
exports.welcome = async (req, res) => {
const tag = req?.body?.fulfillmentInfo?.tag;
let message = tag || req?.query?.message || req?.body?.message || 'Hello World!';
const pool = mysql.createPool({
connectionLimit: 1,
host: '10.30.48.2',
user: 'root',
password: 'TEST@TEST',
database: 'hospital_assist'
});
const jsonResponse = {
fulfillment_response: {
messages: [
{
text: {
//fulfillment text response to be sent to the agent
text: [`Welcome entry point ${message}`],
},
},
],
},
};
let qResult = await pool.query('SELECT * FROM Patients;');
// pool.query('SELECT * FROM Patints', (error, result) => {
// console.log(error);
// console.log(result);
// console.log('In loop');
// res.status(200).send(jsonResponse);
// });
// await pool.query('select * from Patients;', (error, results) => {
// if (error) {
// console.log(error);
// res.status(200).send(jsonResponse);
// }
// qResult = results;
// // res.send(results);
// });
console.log(qResult);
console.log(`In loop ${qResult}`);
res.status(200).send(JSON.stringify(qResult));
// res.status(200).send(jsonResponse);
};
If you're trying to connect to your instance using public IP, you'll need to specify a Unix socket path, eg,如果您尝试使用公共 IP 连接到您的实例,则需要指定 Unix 套接字路径,例如,
mysql.createPool({
user: process.env.DB_USER, // e.g. 'my-db-user'
password: process.env.DB_PASS, // e.g. 'my-db-password'
database: process.env.DB_NAME, // e.g. 'my-database'
// If connecting via unix domain socket, specify the path
socketPath: "/cloudsql/my-project:us-central1:my-instance",
// Specify additional properties here.
...config,
});
In your case, you have several solutions, 2 are specially recommended:对于您的情况,您有几种解决方案,特别推荐 2 种:
Note: with a Cloud SQL public IP avoid to authorized.network on your Cloud SQL database to keep a good level of security注意:使用 Cloud SQL public IP 避免在您的 Cloud SQL 数据库上使用 authorized.network 以保持良好的安全级别
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.