如何在Express路由中将图片路径传递给图片识别函数
解决方法:传递图片路径到imageReko函数
Got it, let's get this sorted out quickly. You just need to adjust your imageReko module to accept a parameter, then use that parameter instead of the hardcoded 'image2.jpg'. Here's how to do it step by step:
1. 修改image-Reko模块,接收图片路径参数
更新你的图片识别模块,让它接收一个参数来代替固定的图片路径:
// image-Reko.js const imageReko = (imagePath) => { // 把原来的固定值替换成传入的参数 const photo = imagePath; // 你的图片识别逻辑代码... }; module.exports = imageReko; // 确保函数已正确导出
2. 确认路由中调用时正确传递参数
你在路由里已经写了imageReko(req.body.img),这部分是正确的,不过可以加个小判断避免空值问题:
const imageReko = require("./image-Reko"); if(req.body.act === "post") { if(req.body.img){ // 这里已正确传递路由接收到的图片路径 imageReko(req.body.img); // 可选:打印日志验证参数是否正确传递 console.log(`Passed image path: ${req.body.img}`); } else { console.warn("No image path provided in request body"); } const post = new Post({ content: req.body.content, firstName:req.body.firstName, lastName: req.body.lastName, img: req.body.img, }); // 记得后续处理post(比如保存到数据库等) }
这样调整后,imageReko函数里的photo变量就会自动替换成从路由传递过来的图片路径字符串,不再是固定的'image2.jpg'了。
内容的提问来源于stack exchange,提问作者Zeyad Alaa Eldin
相关产品推荐
相关产品推荐

