怎么解决的兄弟?我把代码里面的sharp去掉也一样报错
云函数本地调试可用,上传后报错TypeError: Cannot read properties o云函数本地调试可以用,但是上传了就报错 云函数报错 日志内容 Request ID: 2630a398-8b5e-487b-8d82-e42d95cd33a4 执行时间: 0ms内存使用: Zero KB 返回结果 (空) 日志 TypeError: Cannot read properties of undefined (reading 'toString') at writeRuntimeFile (/data/scf/frame/node16/runtime.js:65:37) at main (/data/scf/frame/node16/runtime.js:259:7) at Object.<anonymous> (/data/scf/frame/node16/runtime.js:268:1) at Module._compile (node:internal/modules/cjs/loader:1101:14) at Object.Module._extensions..js (node:internal/modules/cjs/loader:1153:10) at Module.load (node:internal/modules/cjs/loader:981:32) at Function.Module._load (node:internal/modules/cjs/loader:822:12) at Function.executeUserEntryPoint [as runMain] (node:internal/modules/run_main:81:12) at node:internal/main/run_main_module:17:47 START RequestId: 2630a398-8b5e-487b-8d82-e42d95cd33a4 Init Report RequestId: 2630a398-8b5e-487b-8d82-e42d95cd33a4 Coldstart: 7ms (InitRuntime: 7ms InitFunction: 0ms) Memory: 256MB MemUsage: 0.00MB ERROR RequestId: 2630a398-8b5e-487b-8d82-e42d95cd33a4 Result: {"errorCode": -1, "errorMessage": "145 code exit unexpected", "statusCode": 443} END RequestId: 2630a398-8b5e-487b-8d82-e42d95cd33a4 Report RequestId: 2630a398-8b5e-487b-8d82-e42d95cd33a4 Duration: 0ms Memory: 256MB MemUsage: 0.000000MB //云函数代码 const sharp = require('sharp'); const fs = require('fs'); const path = require('path'); const cloud = require('wx-server-sdk'); cloud.init({ env: cloud.DYNAMIC_CURRENT_ENV }); const db = cloud.database(); const processImage = async (tempPath, savePath, targetSizeKB = 100) => { const outputPath = path.join(savePath, `${Date.now()}.jpg`); const maxSize = targetSizeKB * 1024; try { const metadata = await sharp(tempPath).metadata(); let quality = 80; let buffer = await sharp(tempPath) .flatten({ background: '#ffffff' }) // 移除透明背景 .jpeg({ quality }) .toBuffer(); while (buffer.length > maxSize && quality > 30) { quality -= 10; buffer = await sharp(buffer) .jpeg({ quality }) .toBuffer(); } fs.writeFileSync(outputPath, buffer); return outputPath; } catch (err) { return new Error(`图片处理失败: ${err.message}`); } }; exports.main = async (event, context) => { var cloudImgPath = processImage(event.img, event.path) if (typeof (cloudImgPath) === String) { return { code: "100", img: cloudImgPath, msg: "success" } } else { return { code: "500", img: cloudImgPath, msg: "fail" } } }
2025-11-12