收藏
回答

云函数访问MYSQL数据库出错?

// 云函数入口文件

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 个回答

  • Sam
    Sam
    2019-10-18

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

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

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

    也祝你事业兴旺!

    2019-10-18
    有用
    回复
  • 吃包子不吐包子皮
    吃包子不吐包子皮
    2019-10-18

    Cannot read property 'execute' of undefined

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

    2019-10-18
    有用
    回复
登录 后发表内容
问题标签