Cloudinary 图像路径未保存在 MongoDB
Cloudinary Image Path NOT Saved in MongoDB
我正在构建一个 Express 应用程序,它有一个表单,可以选择上传单个图像。我已经使用 cloudinary sdk 为 node.js 实现了图像上传并返回成功。我可以在我的控制台中看到图像 url 并对其进行了测试以确保。喜欢:
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 指南针中,我已经检查过,但图像的值是一个空字符串,如:' '
。这是新创建的产品的片段:
_id: ObjectId("5a9b307937766901104addf8")
title: "Blue Shirt"
slug: "blue-shirt"
price: 123
desc: "a blue ocean"
category: "clothes"
image: ""
__v: 0
我不明白为什么云路径没有保存。请有人帮我解决这个问题。谢谢。
你把你的 Products.findOne()
放到 cloudinary.uploader.upload()
的回调中怎么样?因为上传函数是一个同步调用。所以,你必须让你的储蓄功能等待它。
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);
});
}
});
});
上传到特定文件夹:
首先,转到这个 link https://cloudinary.com/console/settings/upload
并找到“上传预设”部分来创建一个预设,它可以让您选择要使用的文件夹。其次,你的上传代码应该是这样的(我用的是axios)
const fd = new FormData();
fd.append('upload_preset', 'Your preset name');
fd.append('file', file);
return axios.post(API_CLOUDINARY_UPLOAD_URL, fd, { headers: { 'X-Requested-
With': 'XMLHttpRequest' } }).then(result => result.data);
如果您使用的是 Cloudinary 的上传器,根据 Cloudinary 网站:
cloudinary.v2.uploader.unsigned_upload("sample.jpg", "unsigned_1",
{ cloud_name: "demo" },
function(error, result) {console.log(result) });
unsigned_1 是您的预设名称。
完整的文档在这里:Documentation
我希望它能有所帮助。 :)
我正在构建一个 Express 应用程序,它有一个表单,可以选择上传单个图像。我已经使用 cloudinary sdk 为 node.js 实现了图像上传并返回成功。我可以在我的控制台中看到图像 url 并对其进行了测试以确保。喜欢:
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 指南针中,我已经检查过,但图像的值是一个空字符串,如:' '
。这是新创建的产品的片段:
_id: ObjectId("5a9b307937766901104addf8")
title: "Blue Shirt"
slug: "blue-shirt"
price: 123
desc: "a blue ocean"
category: "clothes"
image: ""
__v: 0
我不明白为什么云路径没有保存。请有人帮我解决这个问题。谢谢。
你把你的 Products.findOne()
放到 cloudinary.uploader.upload()
的回调中怎么样?因为上传函数是一个同步调用。所以,你必须让你的储蓄功能等待它。
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);
});
}
});
});
上传到特定文件夹:
首先,转到这个 link https://cloudinary.com/console/settings/upload 并找到“上传预设”部分来创建一个预设,它可以让您选择要使用的文件夹。其次,你的上传代码应该是这样的(我用的是axios)
const fd = new FormData();
fd.append('upload_preset', 'Your preset name');
fd.append('file', file);
return axios.post(API_CLOUDINARY_UPLOAD_URL, fd, { headers: { 'X-Requested-
With': 'XMLHttpRequest' } }).then(result => result.data);
如果您使用的是 Cloudinary 的上传器,根据 Cloudinary 网站:
cloudinary.v2.uploader.unsigned_upload("sample.jpg", "unsigned_1",
{ cloud_name: "demo" },
function(error, result) {console.log(result) });
unsigned_1 是您的预设名称。 完整的文档在这里:Documentation 我希望它能有所帮助。 :)