【发布时间】:2022-02-08 00:56:22
【问题描述】:
我是谷歌云函数的新手,我在这里尝试从云 SQL 加载数据,因为我已经编写了一个代码,该代码具有数据库连接以及 SQL 查询字符串,但是当我尝试测试函数时它会抛出错误或对象或有时它说您没有返回任何数据。我尝试了很多方法来发送数据,但仍然对我不起作用。提前致谢。
代码sn-ps,
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);
};
【问题讨论】:
-
您是否向 Cloud Functions 添加了无服务器 VPC 连接器?您的 Cloud SQL 实例上是否有公共 IP?如果是这样,您的 SQL 引擎是什么(MySQL 或 postgreSQL)?
-
我有公共和私有 IP。 SQL 引擎是 MySQL。
标签: node.js google-cloud-platform google-cloud-functions google-cloud-sql