收藏
回答

云函数操作数据用db.command.in时报错“_ is not defin

框架类型 问题类型 终端类型 AppID 基础库版本
小程序 Bug 客户端 wxfec0f42595e1143f 2.5.2

- 当前 Bug 的表现(可附上截图)

thirdScriptError

_ is not defined;at api cloud.callFunction success callback function

ReferenceError: _ is not defined

- 预期表现


- 复现路径


- 提供一个最简复现 Demo


云函数代码:

// runDB云函数入口文件
const cloud = require('wx-server-sdk')
 
cloud.init()
const db = cloud.database()
const _ = db.command
 
// 云函数入口函数
exports.main = async (event, context) => {
  const targetDB = db.collection(event.db)
  try {
    if (event.type == "insert") {
      return await targetDB.add({
        data: event.data
      })
    }
 
    if (event.type == "update") {
      return await targetDB.doc(event.indexKey).update({
        data: event.data
      })
    }
 
    if (event.type == "delete") {
      return await targetDB.where(event.condition).remove()
    }
 
    if (event.type == "get") {
      return await targetDB.where(event.condition)
        .skip(20 * event.skip)
        .limit(event.limit)
        .get()
    }
  } catch (e) {
    console.error(e)
  }
}

JS

queryCountys: function(queryCountys_condition) {
  var that = this;
  var queryCountys_condition = that.data.queryCountys_condition;
  console.log('调用后的queryCountys_condition数据:' + queryCountys_condition)
 
  wx.cloud.callFunction({
    name: 'runDB',
    data: {
      type: "get", //指定操作是get
      db: "Countys", //指定操作的数据表
      condition: {
        // 指定查询条件
        ParentID: _.in(['XMAMXonnuWjci1r6', 'XMAMpYnnuWjci1r8']),
      },
      limit: 100, // 查询条数
      skip: 0 // 忽略之前的椰树, 比如查询第20到第40条,则指定 1
    },
    success: CountysRec => {
      //数据结果存储为数组
      let CountysArray = CountysRec.result.data;
 
      //设置为页面数据
      that.setData({
        CountysArray,
      });
 
      if (CountysArray.length > 0) {
        console.log(CountysArray)
      }
 
      //如果数组有数据,获取Countys下的数据
      if (CountysArray.length > 0) {
        console.log('CountysArray数组长度:' + CountysArray.length)
        var CountysArrayIndex
      }
    },
    fail: err => {
      console.error('[云函数] [insertDB] 增加Subject失败', err)
    }
  })
},

上述代码报错,但如过不用_in,则正常,如下所示,请大神帮忙!

ParentID: 'XMAMXonnuWjci1r6',


回答关注问题邀请回答
收藏

1 个回答

  • 痛快科技
    痛快科技
    2019-04-25

    云函数和小程序端api是两类,运行在各自的环境中,无法使用你这种思路直接传递condition

    2019-04-25
    有用
    回复 2
    • 2019-04-25

      实测中,不用_in的话,正常。

      2019-04-25
      回复
    • 痛快科技
      痛快科技
      2019-04-25回复

      是的,小程序端condition中的_.in是一个值,确实是没有定义的。定义是在云函数中,两个环境是独立分割的

      2019-04-25
      回复
登录 后发表内容