Node.js 如何使用节点中的multer将图像上载到cloudinary并使用其他数据进行表示

Node.js 如何使用节点中的multer将图像上载到cloudinary并使用其他数据进行表示,node.js,express,multer,cloudinary,Node.js,Express,Multer,Cloudinary,请问我的代码有什么错误?我想设置我的web应用程序,以便当用户通过带有其他文本数据的表单发布图像时,图像将上载到cloudinary,url将返回,并且与用户提交的其他数据一起,所有记录将保存到我的数据库中。请注意,我使用的是pg promise库和postgres DB。我在下面附上我的代码以供参考 我的数据模式 创建表格照片\u表格( post_id uuid引用post(post_id)不为空, 照片由文本引用m_用户(用户名), 标题文本不为空, 说明文本不为空, 照片url文本不为空,

请问我的代码有什么错误?我想设置我的web应用程序,以便当用户通过带有其他文本数据的表单发布图像时,图像将上载到cloudinary,url将返回,并且与用户提交的其他数据一起,所有记录将保存到我的数据库中。请注意,我使用的是pg promise库和postgres DB。我在下面附上我的代码以供参考

我的数据模式

创建表格照片\u表格(
post_id uuid引用post(post_id)不为空,
照片由文本引用m_用户(用户名),
标题文本不为空,
说明文本不为空,
照片url文本不为空,
类别文本,
位置文本,
活动文本,
已在时间戳上发布\u NOT NULL default now(),
更新了时间戳上的_NOT NULL default now(),
主键(pid)
);
我的CLOUDINARY和MULTER配置文件

const cloudinary=require('cloudinary').v2;
const multer=要求(“multer”);
const cloudinaryStorage=require(“multer storage cloudinary”);
cloudinary.config({
云名称:“xxxxxxxx”,
api_键:“XXXXXXXX 3115”,
api_机密:“XXXXXXXXXX YXPMAR3B04”
}); 
const storage=cloudinaryStorage({
cloudinary:cloudinary,
文件夹:“照片”,
允许的格式:['jpg','jpeg','png'],
转换:[{宽度:960,高度:960,裁剪:“限制”}],
文件名:(请求、文件、回调)=>{
const name=file.originalname.split(“”).join(“”);
回调(未定义,名称);
}
});
module.exports=multer({storage:storage}).single('photo');
我的API发布请求

const multer=require('../middleware/multer/photo-config');
//multer在我的路由文件中被调用,如下行所示

router.post('/',multer,phototrl.addPhoto);
下面的函数是我的控制器请求

函数addPhoto(请求、恢复、下一步){
const queryPhoto='INSERT INTO post(post类型,created on)值($1,$2)返回pid'
db.tx('my-transaction',t=>{
返回t.one(queryPhoto,['photo','now()'))
。然后(post=>{
常数数据={
照片作者:req.body.username,
标题:req.body.title,
描述:req.body.description,
照片url:req.files,
类别:req.body.category,
位置:req.body.location,
活动:req.body.campaign
}
const insertPhotoText=`INSERT INTO photo_table(pid,photo_by,title,description,photo_url,category,location,campaign,post_on,updated_on)
价值($1、$2、$3、$4、$5、$6、$7、$8、$9、$10)`
const insertPhotoValues=[post.pid,data.photo_by,data.title,data.description,data.photo_url,data.category,data.location,data.campaign,'now()']
返回t.none(insertPhotoText,insertPhotoValues);
});
})
.然后(函数(){
资源状况(200)
.json({
状态:“成功”,
信息:“添加了一张照片”
});
})
.catch(函数(err){
返回下一个(错误);
})
}

要上传到Cloudinary,需要调用uploader()方法:

示例代码:

app.post('/', upload.any([{ name: "test" }]), (req, res) => {
        var files=req.files;
        if(files){
            files.forEach(function(file){
                cloudinary.uploader.upload(file.path, function(result) { 
                console.log(result);
            });
            });
        }

谢谢@aditi。让我试试这个。但是我的主要挑战来了,因为我正在尝试使用pg promise在事务中发送post请求。