TypeError: response.buffer is not a function

Trying to do some image manipulation in a CloudCode tasks, but I’m running into errors calling Attachment.toBuffer()

console.log(“img”, img.photo); // img.photo clearly exists and is uploaded based on log
const imageBuffer = await img.photo.toBuffer(); // ← this line is throwing the error

Yields this in the CC log:

15:36:52.647 [TASK:INFO] img Attachment {
_urlKey: ‘original’,
id: ‘c7daa3bd-fad0-43dc-9cb8-80116ef1e20b’,
state: ‘uploaded’,
urls: { redacted for brevity }
}
15:36:53.849 [TASK:ERROR] TypeError: response.buffer is not a function
at Attachment.toBuffer (/var/task/app/cloudcode/annotate/node_modules/@journeyapps/cloudcode/node_modules/@journeyapps/db/src/Attachment.ts:147:35)
at processTicksAndRejections (node:internal/process/task_queues:95:5)
at TaskContext.run (/var/task/app/cloudcode/annotate/index.js:10:25)

Looks like a pretty basic use case documented here: Attachments in CloudCode | JourneyApps Docs

Help?

1 Like

Hi @fthomas

This is a known regression with CC 1.14. Options

  1. Downgrade to 1.13
  2. Set your CC task to use “node-fetch” v2.7.0 explicitly and globally (instead of using native fetch which comes standard with 1.14)

To do #2, add the following to the top of your index.ts

// your own imports and requires will go above here (or below I guess)
import _fetch from 'node-fetch';
// @ts-ignore
global.fetch = _fetch;

// rest of your code goes here
export async function run() {}

Manually add node-fetch v2.7.0 to your package.json, update the yarn.lock and deploy

example package.json file

{
  "name": "transfer_packet",
  "cloudcode": {
    "runtime": "1.14.0",
    "enabled": true
  },
  "scripts": {
    "build": "cloudcode-build"
  },
  "dependencies": {
    "@journeyapps/cloudcode": "1.14.0",
    "node-fetch": "2.7.0"
  },
  "devDependencies": {
    "@journeyapps/cloudcode-build": "1.14.0"
  }
}
1 Like

Thanks Tielman - I couldn’t get downgrading to 1.13 to work, but including node-fetch v2.7.0 did work so not going to waste time troubleshooting the former. Have a great weekend!

1 Like