Node.js 云数据库映像路径未保存在MongoDB中
我正在构建一个express应用程序,它有一个表单,可以选择上载单个图像。我已经使用cloudinary sdk for node.js实现了图像上传,并返回了成功。我可以在控制台中看到图像url,并对其进行了测试。比如:Node.js 云数据库映像路径未保存在MongoDB中,node.js,mongodb,image,express,cloudinary,Node.js,Mongodb,Image,Express,Cloudinary,我正在构建一个express应用程序,它有一个表单,可以选择上载单个图像。我已经使用cloudinary sdk for node.js实现了图像上传,并返回了成功。我可以在控制台中看到图像url,并对其进行了测试。比如: Product Cloud Image URL: http://res.cloudinary.com/joey17/image/upload/v1520120085/bte0bhzl6ljcuqmr3vgn.jpg img url value: http:
Product Cloud Image URL: http://res.cloudinary.com/joey17/image/upload/v1520120085/bte0bhzl6ljcuqmr3vgn.jpg
img url value: http://res.cloudinary.com/joey17/image/upload/v1520120085/bte0bhzl6ljcuqmr3vgn.jpg
当我试图在mongodb中保存此路径时,就会出现问题。我用这个片段保存表单中的输入和图像,如下所示:
let imgUrl = "";
cloudinary.uploader.upload(req.files.image.path, (resultImage) => {
console.log('Product Cloud Image URL:\t' + resultImage.url);
imgUrl = resultImage.url;
console.log('img url value:\t' + imgUrl);
});
Products.findOne({slug: slug})
.select('_id title slug image desc price category')
.exec()
.then(product => {
if (product) {
req.flash('danger', 'Product Title Already Exists');
res.render('admin/add_product', {
title: title,
slug: slug,
price: price,
desc: desc,
category: cat,
image: imgUrl
});
} else { // start reading from here... sorry had to include the other parts above
console.log('Unique Product Slug');
let price2 = parseFloat(price).toFixed(2);
let product = new Products({
title: title,
slug: slug,
price: price2,
desc: desc,
category: cat,
image: imgUrl
});
product.save()
.then(data => {
console.log('Product Created:\t' + data);
req.flash('success', 'New Product Created');
res.redirect('/admin/products');
})
.catch(e => {
console.error('Error Saving Product:\t' + e);
});
}
})
.catch(err => {
console.error(err);
});
}
});
在mongo compass中,我进行了检查,但图像的值是空字符串,如:'
。以下是新创建的产品的一个片段:
_id: ObjectId("5a9b307937766901104addf8")
title: "Blue Shirt"
slug: "blue-shirt"
price: 123
desc: "a blue ocean"
category: "clothes"
image: ""
__v: 0
我不明白为什么cloudinary路径没有保存。请有人帮我解决这个问题。谢谢。把你的
产品.findOne()
放到cloudinary.uploader.upload()
的回调中怎么样。因为上传函数是一个同步调用。因此,您必须让您的保存功能等待它
让imgUrl=”“;
cloudinary.uploader.upload(req.files.image.path,(resultImage)=>{
log('Product Cloud Image URL:\t'+resultImage.URL);
imgUrl=resultImage.url;
log('imgUrl值:\t'+imgUrl);
Products.findOne({slug:slug})
.选择(“\u id title slug image desc price category”)
.exec()
。然后(产品=>{
if(产品){
要求闪存(“危险”,“产品名称已存在”);
res.render('管理/添加产品'{
标题:标题,,
鼻涕虫:鼻涕虫,
价格:价格,
描述:描述,
类别:猫,,
图片:imgUrl
});
}否则{//从这里开始阅读……对不起,必须包括上面的其他部分
console.log(“唯一的产品Slug”);
让price2=parseFloat(price).toFixed(2);
让产品=新产品({
标题:标题,,
鼻涕虫:鼻涕虫,
价格:价格2,
描述:描述,
类别:猫,,
图片:imgUrl
});
product.save()
。然后(数据=>{
log('创建的产品:\t'+数据);
要求flash(“成功”,“新产品创建”);
res.redirect('/admin/products');
})
.catch(e=>{
console.error('保存产品时出错:\t'+e);
});
}
})
.catch(错误=>{
控制台错误(err);
});
}
});
});
My bad,我应该将产品保存在cloudinary回调中。第一个对此答案发表评论的人将被接受。提前谢谢。是的,我知道了。所以没有实时重新加载。感谢好友可以在线将图像保存到文件夹中吗?可以,您可以转到Cloudinary设置部分设置上载预设,以便分配文件夹。当你编码上传图像时,你可以选择使用不同的预设来控制你想上传到哪个文件夹。我已经检查了文档和控制台,但不明白它。你能分享一个例子吗?很难用这个注释来显示代码。所以,我将在我的原始答案中发布它。