云函数访问MYSQL数据库出错?
发布于 5 年前 作者 ezhong 6190 次浏览 来自 官方Issues

// 云函数入口文件

const cloud = require(‘wx-server-sdk’)

const mysql=require(‘mysql2/promise’)

cloud.init()

// 云函数入口函数

exports.main = async (event, context) => {

const connection=await mysql.createConnection({

host:mysql57.rdsmj85epr606zi.rds.bj.baidubce.com,

user:‘sam’,

database:‘samMYSQL’,

password:‘xxxxxxxx’,

})

const [rows, fields] = await connection.execute(‘SELECT version();’)

return rows

}

错误如下:

WAService.js:1 Uncaught (in promise) Error: errCode: -404011 cloud function execution error | errMsg: cloud.callFunction:fail requestID 8a0ca2a7-f0f0-11e9-af3a-525400192d0e, cloud function service error code -504002, error message Cannot read property ‘execute’ of undefined; at cloud.callFunction api; 

    at new u (WAService.js:1)

    at d (WAService.js:1)

    at f (WAService.js:1)

    at Function.success (WAService.js:1)

    at WAService.js:1

    at C (WAService.js:1)

    at i.<anonymous> (WAService.js:1)

    at i.emit (WAService.js:1)

    at Rs (WAService.js:1)

    at WAService.js:1

2 回复

刚刚,我重新上传一个新的云函数,同样的代码,npm install mysql2,居然神奇的成功了。

我认为,是因为前一个云函数,我曾经删去过一次目录的因素。

还是很谢谢你给我带来的好运。

也祝你事业兴旺!

Cannot read property ‘execute’ of undefined

这个js语法错误 应该是 connection 是 undefined吧

回到顶部