云函数报错 $.pipeline is not a function ?
发布于 5 年前 作者 caiming 11091 次浏览 来自 官方Issues
const cloud = require'wx-server-sdk')

cloud.init({
  env: cloud.DYNAMIC_CURRENT_ENV
})

const db = cloud.database()

const _ = db.command

const $ = db.command.aggregate

// 云函数入口函数
exports.main = async (event, context) => {

省略....

const res = await db.collection('orders')
    .aggregate()
    .addFields(fields)
    .match(conditions)
    .lookup({
      from: 'users',
      let: {
        _openid: '$_openid'
      },
      pipeline: $.pipeline()
        .match(_.expr($.eq(['$_openid', '$$_openid'])))
        .project({
          _id: 0,
          staffNickName: 1,
          nickName: 1
        })
        .done(),
      as: 'userList'
    })
    .sort({
      addTime: -1
    })
    .skip(skip)
    .limit(100)
    .addFields({
      addTimeS: $.dateToString({
        date: '$addTime',
        format: '%Y-%m-%d %H:%M:%S',
        timezone: 'Asia/Shanghai'
      })
    })
    .end()

详细错误日志如下:

START RequestId: a03972e1-311-11ea-a6f9-525400235f2a
Event RequestId: a03972e1-311-11ea-a6f9-525400235f2a
TypeError: $.pipeline is not a function
    at EventHandler.exports.main [as realHandler] (/var/user/index.js:11719)
    at EventHandler.handle (/var/runtime/node8/bootstrap.js:40528)
    at invoke (/var/runtime/node8/bootstrap.js:20822)
    at Timeout.setTimeout [as _onTimeout] (/var/runtime/node8/bootstrap.js:137)
    at ontimeout (timers.js:47511)
    at tryOnTimeout (timers.js:310)
    at Timer.listOnTimeout (timers.js:270)
 
END RequestId: a03972e1-311-11ea-a6f9-525400235f2a
Report RequestId: a03972e1-311-11ea-a6f9-525400235f2a Duration:ms Memory:256MB MaxMemoryUsed:34.148438MB

如标题提示错误,大家遇到过吗?我目的是让子查询输出字段少一些。如果绕开pipeline,还可以用什么办法?

3 回复

先试试简单的lookup有没有报错

await db.collection('orders').aggregate()
    .lookup({
      from: 'users',
      let: {
        _openid: '$_openid'
      },
      pipeline: $.pipeline()
        .match(_.expr($.eq(['$_openid', '$$_openid'])))
        .project({
          _id: ,
          staffNickName: ,
          nickName: 
        })
        .done(),
      as: 'userList'
    })
    .end()
const $ = db.command.aggregate

另外我发现一个小问题:

let: {
        _openid: '$_openid'
      }

这里的参数里面不能带下划线_,会报错,去掉_可以。

回到顶部