收藏
回答

云函数里如何随机获取数据库的一个记录的某一个字段?

在云函数里随机获取数据库的一个记录的某一个字段,但是报错了


代码如下:

let word = await db.collection('word')

.aggregate()

.sample({

size: 1

})

.end()

// .then(res => {console.log("随机数结果",res.data[0].eng,res.data[0].chs); })

.then(res => { console.log("随机数结果", res); })

.catch(err => {console.log("随机数错误",err); })

console.log("中文",word.data[0].chs)


返回结果:

{"errorCode":1,"errorMessage":"user code exception caught","stackTrace":"Cannot read property 'data' of undefined"}

日志:

START RequestId: f2f88334-f188-11e9-aabf-525400697544

Event RequestId: f2f88334-f188-11e9-aabf-525400697544

2019-10-18T09:23:30.696Z f2f88334-f188-11e9-aabf-525400697544 随机数结果 { list: [ { _id: 'XakbZfdsX1oQevCz', chs: '钢笔', eng: 'pen' } ],

  errMsg: 'collection.aggregate:ok' }

TypeError: Cannot read property 'data' of undefined

    at EventHandler.exports.main [as realHandler] (/var/user/index.js:28:27)

    at <anonymous>

    at process._tickCallback (internal/process/next_tick.js:188:7)

END RequestId: f2f88334-f188-11e9-aabf-525400697544

Report RequestId: f2f88334-f188-11e9-aabf-525400697544 Duration:38ms Memory:256MB MaxMemoryUsed:30.636719MB


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

3 个回答

  • 老张
    老张
    2019-10-21

    语法错误吧?

    await怎么还有then?

    let word = await db.collection('word').aggregate().sample({size: 1}).end()

    console.log("中文",word.data[0].chs)


    2019-10-21
    有用
    回复 1
    • 吃碗饭就快活起来了
      吃碗饭就快活起来了
      2019-10-21
      找到问题了,改成 word.list[0].chs 就行了,加上await应该不冲突吧,我看到也能运行
      2019-10-21
      回复
  • 吃碗饭就快活起来了
    吃碗饭就快活起来了
    2019-10-21

    我这样打印是没问题的console.log("中文",word)

    但是我如果变成这样就报错了

    console.log("中文",word.data[0].chs)

    就是这个: Cannot read property '0' of undefined

    这个要怎么取出一个字段的信息?

    2019-10-21
    有用
    回复
  • ulysses
    ulysses
    2019-10-18

    2019-10-18T09:23:30.696Z f2f88334-f188-11e9-aabf-525400697544 随机数结果 { list: [ { _id: 'XakbZfdsX1oQevCz', chs: '钢笔', eng: 'pen' } ],

    结果已经出来了,显然是代码问题,这里没返回值


    .then(res => { console.log("随机数结果", res); })

    .then(res => { console.log("随机数结果", res); return res })


    2019-10-18
    有用
    回复 3
    • 吃碗饭就快活起来了
      吃碗饭就快活起来了
      2019-10-18
      不是这个问题,我定义了一个云函数定时触发器,每隔10s从数据库随机获取信息并打印出来,是后面console.log("中文",word.data[0].chs)这段代码报错了,但是不知道为什么会报错
      2019-10-18
      回复
    • ulysses
      ulysses
      2019-10-18回复吃碗饭就快活起来了
      就是因为你的 .then 里面没返回,所以 后边拿不到结果了啊。.then 里面有输出内容,说明数据库操作已经返回结果了
      2019-10-18
      回复
    • 吃碗饭就快活起来了
      吃碗饭就快活起来了
      2019-10-20回复ulysses
      改成如下还是报错:
        let word = await db.collection('word')
          .aggregate()
          .sample({
            size: 1
          })
          .end()
          // .then(res => {console.log("随机数结果",res.data[0].eng,res.data[0].chs); })
          .then(res => { console.log("随机数结果", res); return res})
          .catch(err => {console.log("随机数错误",err); })

          console.log("中文",word.data[0].chs)


      日志如下:
      START RequestId: 3338ea66-f2eb-11e9-9949-525400e24e59
      Event RequestId: 3338ea66-f2eb-11e9-9949-525400e24e59
      2019-10-20T03:39:20.316Z 3338ea66-f2eb-11e9-9949-525400e24e59 随机数结果 { list: [ { _id: 'XakbP0_Mv-8c5-iR', chs: '苹果', eng: 'apple' } ],
        errMsg: 'collection.aggregate:ok' }
      TypeError: Cannot read property '0' of undefined
          at EventHandler.exports.main [as realHandler] (/var/user/index.js:26:31)
          at
          at process._tickCallback (internal/process/next_tick.js:188:7)
      END RequestId: 3338ea66-f2eb-11e9-9949-525400e24e59
      Report RequestId: 3338ea66-f2eb-11e9-9949-525400e24e59 Duration:28ms Memory:256MB MaxMemoryUsed:34.648438MB
      2019-10-20
      回复
登录 后发表内容
问题标签