这个错误是因为在云函数中使用了不支持的ES模块导入方式。解决方案是将fetch模块的导入方式改为动态导入。具体修改代码如下:

const tcb = require('@cloudbase/node-sdk')
const app = tcb.init({
  env: 'lowcode-0gtbzdgf5319e5ba'
})
const db = app.database()
const $ = db.command.aggregate
const _ = db.command

exports.main = async (event, context) => {
  const result = await db.collection('lcap-data-37nRRzZ3q-qlxx_91vp39c').where({ msg: "加入成功" })
    .skip(1500)
    .limit(100)
    .get()

  console.log("数据长度:" + result.data.length)

  const fetchPromises = result.data.map((item) => {
    return import('node-fetch').then(({ default: fetch }) => {
      return fetch("https://edith.xiaohongshu.com/api/redcity/v1/partner/group/join?sid=1691152628069138757239", {
        method: "POST",
        headers: {
          "xy-common-params": "sid=1691152628069138757239&build=7150300",
          "Content-Type": "application/json",
        },
        body: JSON.stringify({ "groupId": item.group_id, "source": 17 })
      })
      .then(() => {
        console.log("请求成功")
      })
      .catch((error) => {
        console.log("请求失败:" + error)
      });
    });
  })

  await Promise.all(fetchPromises)

  return {
    i: 1
  }
};

将const fetch = require('node-fetch');改为return import('node-fetch').then(({ default: fetch }) => {,然后在Promise.all(fetchPromises)之前加上await关键字


原文地址: https://www.cveoy.top/t/topic/iutv 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录