云开发用的云函数中调用mysql日志一直报调用失败?
背景:云开发、云函数。由于云开发的db操作实在太麻烦,于是采用了自己搭建的mysql。然后小程序通过请求云函数,云函数再连接mysql获取需要的数据。 问题:云函数连接mysql获取到数据,前端显示日志正常,但云开发的云函数日志却显示调用失败。同样的requestID! 截图如下: [图片] 代码片段: const cloud = require('wx-server-sdk')
//引入mysql操作模块
const mysql = require('mysql2/promise')
cloud.init({
env: cloud.DYNAMIC_CURRENT_ENV
})
// 云函数入口函数
exports.main = async (event, context) => {
//链接mysql数据库,这里你可以链接你mysql中的任意库
try {
const wxContext = cloud.getWXContext()
const openid = wxContext.OPENID
const connection = await mysql.createConnection({
host: "220.248.*.*",
database: "bv_xxx",
port: 3306,
user: "username",
password: "pwd***"
})
console.log('event=', event)
var offset = event.offset ? event.offset : 0;
var pageSize = event.pageSize ? event.pageSize : 20;
var apiType = event.apiType;
//1:获取文章列表
//2:获取视频列表
//3:收藏列表
//4:收藏/取消收藏
switch (apiType) {
case 1: //获取文章列表
var sql = 'select ar.*,(select count(1) from bv_collect where openid=\"' + openid + '\" and aid=ar.id) as collect from bv_article ar where ar.is_delete = 0 order by ar.create_time desc limit ' + offset + ',' + pageSize + ';'
console.log('sql=', sql)
var [rows, fields] = await connection.execute(sql)
// console.log('rows=', rows)
return rows;
case 2: //获取视频列表
var sql = 'select * from bv_video where is_delete = 0 order by create_time;'
var [rows, fields] = await connection.execute(sql)
// console.log('rows=', rows)
return rows;
}
return '格式错误'
} catch (err) {
console.log("链接错误", err)
return err
}
}