报错信息如下
14:08:41.622 [本地调试]FaasError: Error: self-signed certificate in certificate chain
14:08:41.622 [本地调试] at process.processTicksAndRejections (node:internal/process/task_queues:95:5)
分析原因
整体代码如下
const db = uniCloud.database()
const user = db.collection('uni-id-users')
exports.main = async (event, context) => {
const res_user = await user.where({
openid: 122
}).get()
//返回数据给客户端
return event
};
分析原因可能是在公司内网环境或者电脑有什么设置导致ssl证书异常
解决办法
在开发环境中禁用 SSL 校验(解决自签名证书问题)
'use strict';
const db = uniCloud.database()
const user = db.collection('uni-id-users')
// 禁用 SSL 校验(仅限开发环境)
process.env['NODE_TLS_REJECT_UNAUTHORIZED'] = '0';
exports.main = async (event, context) => {
const res_user = await user.where({
openid: 122
}).get()
//返回数据给客户端
return event
};
重新编译运行,此时提示一个警告: Warning: Setting the NODE_TLS_REJECT_UNAUTHORIZED environment variable to '0' makes TLS connections and HTTPS requests insecure by disabling certificate verification.
将NODE_TLS _REJECT_UNAUTHORIZED环境变量设置为“0”会禁用证书验证,从而使TLS连接和HTTPS请求不安全。
此时云函数数据库操作可以正常调通
添加完删除禁用 SSL 校验代码,依旧可以正常运行,避免将代码提交到线上环境