Cloud Functions with Cloud SQL - 属性 'socketPath' 类型不存在
Cloud Functions with Cloud SQL - Property 'socketPath' does not exist on type
我正在尝试在我的 Firebase 函数上访问我的 Google 云 SQL 数据库。我跟着 Google documentation 但是它不是很好并且遗漏了很多信息。
我将文档中的代码添加到我的 src\index.ts
文件并填写了我的数据:
const mysql = require('mysql');
const connectionName =
process.env.INSTANCE_CONNECTION_NAME || 'test';
const dbUser = process.env.SQL_USER || 'test';
const dbPassword = process.env.SQL_PASSWORD || 'test';
const dbName = process.env.SQL_NAME || 'test';
const mysqlConfig = {
connectionLimit: 1,
user: dbUser,
password: dbPassword,
database: dbName,
};
if (process.env.NODE_ENV === 'production') {
mysqlConfig.socketPath = `/cloudsql/${connectionName}`;
}
// Connection pools reuse connections between invocations,
// and handle dropped or expired connections automatically.
let mysqlPool;
exports.mysqlDemo = (req, res) => {
// Initialize the pool lazily, in case SQL access isn't needed for this
// GCF instance. Doing so minimizes the number of active SQL connections,
// which helps keep your GCF instances under SQL connection limits.
if (!mysqlPool) {
mysqlPool = mysql.createPool(mysqlConfig);
}
mysqlPool.query('SELECT NOW() AS now', (err, results) => {
if (err) {
console.error(err);
res.status(500).send(err);
} else {
res.send(JSON.stringify(results));
console.log("Connected!")
}
});
// Close any SQL resources that were declared inside this function.
// Keep any declared in global scope (e.g. mysqlPool) for later reuse.
};
然后我尝试发布它以查看它是否有效,但我收到了错误消息:
Module 'mysql' is not listed as dependency in package.json
所以我通过 npm install mysql
安装了 mysql
我又 运行 一次,这次我得到错误:
src/index.ts(23,15): error TS2339: Property 'socketPath' does not exist on type '{ connectionLimit: number; user: string; password: string; database: string; }'
这是怎么回事?我安装正确了吗?我很迷茫,文档似乎没有提供太多解决方案。谢谢!
这似乎是一个 TypeScript 错误,其中正在推断您的配置对象的接口,然后您试图在该对象上添加一个未声明的额外字段。换句话说,来自 Google 的示例代码是 JavaScript,您正在使用 TypeScript 并且 运行 进入 TS 错误。
我 认为 您应该能够调整 mysqlConfig
的声明以包含 socketPath
属性(设置为 null
) 一切都应该没问题。换句话说,将 mysqlConfig
定义更改为:
const mysqlConfig = {
connectionLimit: 1,
user: dbUser,
password: dbPassword,
database: dbName,
socketPath: null
};
我正在尝试在我的 Firebase 函数上访问我的 Google 云 SQL 数据库。我跟着 Google documentation 但是它不是很好并且遗漏了很多信息。
我将文档中的代码添加到我的 src\index.ts
文件并填写了我的数据:
const mysql = require('mysql');
const connectionName =
process.env.INSTANCE_CONNECTION_NAME || 'test';
const dbUser = process.env.SQL_USER || 'test';
const dbPassword = process.env.SQL_PASSWORD || 'test';
const dbName = process.env.SQL_NAME || 'test';
const mysqlConfig = {
connectionLimit: 1,
user: dbUser,
password: dbPassword,
database: dbName,
};
if (process.env.NODE_ENV === 'production') {
mysqlConfig.socketPath = `/cloudsql/${connectionName}`;
}
// Connection pools reuse connections between invocations,
// and handle dropped or expired connections automatically.
let mysqlPool;
exports.mysqlDemo = (req, res) => {
// Initialize the pool lazily, in case SQL access isn't needed for this
// GCF instance. Doing so minimizes the number of active SQL connections,
// which helps keep your GCF instances under SQL connection limits.
if (!mysqlPool) {
mysqlPool = mysql.createPool(mysqlConfig);
}
mysqlPool.query('SELECT NOW() AS now', (err, results) => {
if (err) {
console.error(err);
res.status(500).send(err);
} else {
res.send(JSON.stringify(results));
console.log("Connected!")
}
});
// Close any SQL resources that were declared inside this function.
// Keep any declared in global scope (e.g. mysqlPool) for later reuse.
};
然后我尝试发布它以查看它是否有效,但我收到了错误消息:
Module 'mysql' is not listed as dependency in package.json
所以我通过 npm install mysql
mysql
我又 运行 一次,这次我得到错误:
src/index.ts(23,15): error TS2339: Property 'socketPath' does not exist on type '{ connectionLimit: number; user: string; password: string; database: string; }'
这是怎么回事?我安装正确了吗?我很迷茫,文档似乎没有提供太多解决方案。谢谢!
这似乎是一个 TypeScript 错误,其中正在推断您的配置对象的接口,然后您试图在该对象上添加一个未声明的额外字段。换句话说,来自 Google 的示例代码是 JavaScript,您正在使用 TypeScript 并且 运行 进入 TS 错误。
我 认为 您应该能够调整 mysqlConfig
的声明以包含 socketPath
属性(设置为 null
) 一切都应该没问题。换句话说,将 mysqlConfig
定义更改为:
const mysqlConfig = {
connectionLimit: 1,
user: dbUser,
password: dbPassword,
database: dbName,
socketPath: null
};