云函数操作数据用db.command.in时报错“_ is not defin
发布于 5 年前 作者 zengguiying 14828 次浏览 来自 问答
  • 当前 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 回复

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

回到顶部