soloEmperor
soloEmperor
  • 发布:2020-04-14 17:35
  • 更新:2020-04-16 08:58
  • 阅读:1806

云函数调云函数报错

分类:uniCloud
'use strict';  
const db = uniCloud.database()  
exports.main = async (event, context) => {  
    //event为客户端上传的参数  
    const collection = db.collection('user')  
       //根据官网示例调用云函数  
    let callFunctionResult = await uniCloud.callFunction({  
        name: "queryUser",  
        data: {  
            mobilePhone:event.mobilePhone  
        }  
    })  
    console.log('callFunctionResult',callFunctionResult)  
    return event  
};

--------------报错如下:
[uniappCloud] 17:28:40.513 [腾讯云:solo-cloud]运行状态:失败
[uniappCloud] 17:28:40.514 [腾讯云:solo-cloud]返回结果:{"errorCode":-1,"errorMessage":"user code exception caught","stackTrace":"errCode: FUNCTIONS_EXECUTE_FAIL | errMsg: errCode: SIGN_PARAM_INVALID | errMsg: signature calculated is different from client signature\nError: errCode: SIGN_PARAM_INVALID | errMsg: signature calculated is different from client signature\n at t.then.t (/var/user/@dcloudio/serverless/lib/tcb/uni-cloud.js:1:1182)\n at \u003canonymous\u003e\n at process._tickCallback (internal/process/next_tick.js:188:7)\nError: errCode: FUNCTIONS_EXECUTE_FAIL | errMsg: errCode: SIGN_PARAM_INVALID | errMsg: signature calculated is different from client signature\nError: errCode: SIGN_PARAM_INVALID | errMsg: signature calculated is different from client signature\n at t.then.t (/var/user/@dcloudio/serverless/lib/tcb/uni-cloud.js:1:1182)\n at \u003canonymous\u003e\n at process._tickCallback (internal/process/next_tick.js:188:7)\n at t.then.t (/var/user/@dcloudio/serverless/lib/tcb/uni-cloud.js:1:1182)\n at \u003canonymous\u003e\n at process._tickCallback (internal/process/next_tick.js:188:7)"}
[uniappCloud] 17:28:40.514 [腾讯云:solo-cloud]运行日志:
Error: errCode: FUNCTIONS_EXECUTE_FAIL | errMsg: errCode: SIGN_PARAM_INVALID | errMsg: signature calculated is different from client signature
Error: errCode: SIGN_PARAM_INVALID | errMsg: signature calculated is different from client signature
at t.then.t (/var/user/@dcloudio/serverless/lib/tcb/uni-cloud.js:1:1182)
at <anonymous>
at process._tickCallback (internal/process/next_tick.js:188:7)
at t.then.t (/var/user/@dcloudio/serverless/lib/tcb/uni-cloud.js:1:1182)
at <anonymous>
at process._tickCallback (internal/process/next_tick.js:188:7)
[uniappCloud] 17:28:40.514 [腾讯云:solo-cloud]运行报告:计费时间:200ms 计费时间:125ms 运行内存:34.5MB

2020-04-14 17:35 负责人:无 分享
已邀请:
DCloud_uniCloud_WYQ

DCloud_uniCloud_WYQ

云函数queryUser里面怎么写的呢

  • soloEmperor (作者)

    'use strict';

    const db = uniCloud.database()

    const dbCmd = db.command

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

    console.log('event',event)

    let res = await db.collection('user').where({

    mobilePhone: dbCmd.eq(event.mobilePhone)

    }).get()

    if (res.data) {

    console.log('data',res.data)

    return res;

    } else {

    console.log('no-data')

    return {

    code:'400',

    data:{},

    msg:'找不到该用户'

    }

    }

    };


    ----这是queryUser的代码,大致是用mobilePhone作为查询条件去查询

    2020-04-15 13:40

  • DCloud_uniCloud_WYQ

    回复 soloEmperor: 有使用signInWithTicket什么的吗

    2020-04-15 15:32

  • soloEmperor (作者)

    回复 DCloud_uniCloud_WYQ: 还没。刚开始写云函数增删改查之类的,还没到这一步来

    2020-04-15 17:56

  • 叶隐霜

    回复 soloEmperor: 兄弟,你问题解决了吗,我碰到一样的问题了

    2020-05-01 10:44

  • soloEmperor (作者)

    回复 叶隐霜: 没,不能调,只能又继续写方法

    2020-05-08 15:52

h***@163.com

h***@163.com

没有用过腾讯云,请问下如何开通的?
看代码似乎是要访问数据库取数
个人的愚见
云函数:

'use strict';    
const db = uniCloud.database()    
exports.main = async (event, context) => {    
    //event为客户端上传的参数    
    const collection = db.collection('user')    
       //这里直接去数据库取数,没有再调用云函数的必要(仅看目前的代码)  
    let res= await collection.where({  
          mobilePhone:event.mobilePhone   
         }) .get()  
    console.log(res)    
    return res  
};

前端调用:

let callFunctionResult = await uniCloud.callFunction({    
        name: "queryUser",  //这里是云函数的名称  
        data: {    
            mobilePhone:this.mobilePhone  //这个是前端要传到后端的数据,具体从哪里来不知道  
        }    
    })    
console.log(callFunctionResult )
  • soloEmperor (作者)

    是这样子的,我想实现的逻辑是:

    有 addUser 和 queryUser两个云函数;

    前端调addUser传入user:{userName:'',mobilePhone:''},然后addUser中调queryUser并传入user.mobilePhone,queryUser查询出结果,如果存在,则返回给addUser,addUser再返给前端告知已存在不能重复添加,如果存在,则addUser直接把数据插入数据库。

    2020-04-15 13:46

h***@163.com

h***@163.com

既然如此,queryUser这个函数是不需要的,因为云函数是可以直接访问数据库的,直接在addUser 中调用数据查询方法即可获取结果,再根据结果判断是插入还是返回。

  • soloEmperor (作者)

    嗯,我现在就是这样解决的。但是这样的话,类似queryUser这种条件查询的云函数就不能作为公用云函数了。比如queryUser如果是多条件查询,可以供其他函数调用,不是更好

    2020-04-15 17:55

h***@163.com

h***@163.com

如果要调用公用云函数,请参考官方的文档。阿里云
在公用模块内exports,在云函数内require即可。示例代码如下:

//公用函数  
// common/hello-common/index.js  
function getVersion() {  
  return '0.0.1'  
}  
module.exports = {  
  getVersion,  
  secret: 'your secret'  
}
// useCommon/index.js  
'use strict';  
//在这里引入  
const {secret,getVersion} = require('hello-common')  
exports.main = async (event, context) => {  
  let version = getVersion()  
  return {  
    secret,  
    version  
  }  
}

该问题目前已经被锁定, 无法添加新回复