![](/img/trans.png)
[英]How to read CSV data stored in Google Cloud Storage with Cloud Functions
[英]How to read data form cloud SQL in Google Cloud Function in Node JS
我是 Google Cloud Function 的新手,我在这里尝试从云 SQL 加载数据,因为我编写了一个代码,它具有数据库连接以及 SQL 查询字符串但是当我尝试测试 function 时,它抛出错误或 Object或者有时它说您没有返回任何数据。 我尝试了很多发送数据的方法,但仍然对我不起作用。 提前致谢。
代码片段,
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);
};
如果您尝试使用公共 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,
});
有关详细信息,请参阅文档。
对于您的情况,您有几种解决方案,特别推荐 2 种:
注意:使用 Cloud SQL public IP 避免在您的 Cloud SQL 数据库上使用 authorized.network 以保持良好的安全级别
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.