云函数操作数据库,为什么返回NULL,什么结果都没有哪里有问题?

小程序 文章 2021-01-31 16:21 0 17 全屏看文

云函数操作数据库,为什么返回NULL,什么结果都没有哪里有问题?Why does the cloud function return NULL when operating the database? There is no problem with any result?

云函数操作数据库,为什么返回NULL,什么结果都没有哪里有问题?

// 云函数模板

// 部署:在 cloud-functions/login 文件夹右击选择 “上传并部署”


const cloud = require('wx-server-sdk')

// 初始化 cloud

cloud.init({

  // API 调用都保持和云函数当前所在环境一致

  env: "anjianteam-7gwp9t1fd42848b0"

})

const db = cloud.database()

const loguser = "Ts51102186"

const logpwd = "Ts51102186"

/** * 这个示例将经自动鉴权过的小程序用户 openid 返回给小程序端 * 

 * event 参数包含小程序端调用传入的 data*  */

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

  const wxContext = cloud.getWXContext()

  const openid =wxContext.OPENID //存储openid

  let usInfo = event.userInfo //存储参数userinfo

  console.log(wxContext.OPENID)  

  if (event.loguser === loguser && event.logpwc === logpwd{ //校验用户名密码

    console.log('第2步')

    //添加userInfo数据开始

   // let userInfo = event.userInfo

    const _ = db.command

    let searchres = await db.collection('userInfo').where({

      _id: openid

    }).get();

   console.log('打印查询数',searchres)    //判断是否存在

        

    if (searchres.data.length === 0{

      console.log('不存在该用户add')

      db.collection('userInfo').add({

        data: {

          _id: openid,

          userInfo: usInfo

        },

        success: function (res) {

          console.log(res)

          return openid

        }

      })

    }

  }

}

//Cloud function template / / deployment: right click in the cloud functions / login folder and select "upload and deploy" const cloud = require ('wx server SDK ') / / initialize cloud cloud.init ({/ / API calls are consistent with the current environment of cloud functions env: "anjianteam-7gwp9t1fd42848b0"}) const DB= cloud.database () const loguser = "Ts51102186" const logpwd = "Ts51102186" / * * this example returns the automatically authenticated openid of the applet user to the applet side * * the event parameter contains the data * that the applet side calls in*/ exports.main = async (event, context) => { const wxContext = cloud.getWXContext () const openid = wxContext.OPENID //Store openid let usinfo= event.userInfo //Store parameter userinfo console.log ( wxContext.OPENID ) if ( event.loguser === loguser && event.logpwc ===Logpwd) {/ / verify user name and password console.log ('step 2 ') / / start adding userinfo data / / let userinfo= event.userInfo const _ = db.command let searchres = await db.collection ('userInfo').where({ _ id: openid }).get(); console.log ('number of print queries', searches) / / judge whether there is an if( searchres.data.length === 0) { console.log ('the user does not exist add ') db.collection ('userInfo').add({ data: { _ id: openid, userInfo: usInfo }, success: function (res) { console.log (res) return openid } }) } } }

回答:

陈宇明:

自己感受一下

跨商通:

看得眼瞎。切换到亮模式后再贴代码吧。

不尊重别人,别人也不会给你解答的。

Mr.Zhao:

发给你了,就不照着做,想不明白

-EOF-