mongoose 和 cloudinary:如何使用 public_id 查找文件?
mongoose and cloudinary: how to use the public_id to find the file?
我正在使用以下代码查找所有文档:
const getAllCv = async (req, res) => {
CV.find({}, function(err, res){
const getResponseFromMongoDb = {};
res.forEach(function(index){
getResponseFromMongoDb[index] = index;
})
console.log(getResponseFromMongoDb.cv_file);
});
//console.log(getResponseFromMongoDb);
}
示例响应是:
{
_id: 60597b9454893a1a9c16f3c5,
name: 'John Doe',
contact: '12345678912',
email: 'john@gmail.com',
shipType: 'BULK CARRIER',
rank: 'CHIEEF ENGINEER',
cv_file: 'lm3iyxn0pbu9tuheicfr',
date: 2021-03-23T05:24:36.906Z,
__v: 0
}
我需要单独跟踪整个响应和 cv_file。 cv 文件包含我用作文件存储系统的 cloudinary public_id。
所以我需要类似的东西
const cv_file_id = res.cv_file -------> to find the fileassociated with the name/contact
## find the file from cloudinary
最后做出单一响应,其中包含我可用于我的管理仪表板的文件的名称、联系人、电子邮件、shipType、级别、缓冲区。
现在如何从我现有的响应中进行单独的查询并进行单个 json 响应?
我的方法正确吗?还有其他更简单的方法吗?
N.B:我的管理仪表板在后端,所以我使用 ejs 作为模板引擎
这很简单。您所要做的就是按照格式将 public_id 附加到 cloudinary 的 URL 中。
这是上传图片后来自 cloudinary 的示例响应(我省略了很多字段以保持答案准确):-
{
"public_id": "eneivicys42bq5f2jpn2",
"version": 1570979139,
"width": 1000,
"height": 672,
"format": "jpg",
"resource_type": "image",
"type": "upload",
"placeholder": false,
"url": "http://res.cloudinary.com/demo/image/upload/v1570979139/eneivicys42bq5f2jpn2.jpg",
"secure_url": "https://res.cloudinary.com/demo/image/upload/v1570979139/eneivicys42bq5f2jpn2.jpg",
"access_mode": "public",
"original_filename": "sample",
}
完整样本响应 here
如您所见,public_id 是图像 URL 中的最后一个参数(在 URL 和 secure_url 字段中给出)以及其他参数,例如版本、图片格式、云名称等
总之:-
您可以使用版本、public_id、云名称等构建完整的 URL,或者仅将响应中的 secure_url 存储在您的 JSON 文档中.
您所要做的就是向客户端发送相同的响应并在那里获取 image/file。您可以向管理员提供 link 本身或作为 <img src={url_of_file} />
我正在使用以下代码查找所有文档:
const getAllCv = async (req, res) => {
CV.find({}, function(err, res){
const getResponseFromMongoDb = {};
res.forEach(function(index){
getResponseFromMongoDb[index] = index;
})
console.log(getResponseFromMongoDb.cv_file);
});
//console.log(getResponseFromMongoDb);
}
示例响应是:
{
_id: 60597b9454893a1a9c16f3c5,
name: 'John Doe',
contact: '12345678912',
email: 'john@gmail.com',
shipType: 'BULK CARRIER',
rank: 'CHIEEF ENGINEER',
cv_file: 'lm3iyxn0pbu9tuheicfr',
date: 2021-03-23T05:24:36.906Z,
__v: 0
}
我需要单独跟踪整个响应和 cv_file。 cv 文件包含我用作文件存储系统的 cloudinary public_id。
所以我需要类似的东西
const cv_file_id = res.cv_file -------> to find the fileassociated with the name/contact
## find the file from cloudinary
最后做出单一响应,其中包含我可用于我的管理仪表板的文件的名称、联系人、电子邮件、shipType、级别、缓冲区。
现在如何从我现有的响应中进行单独的查询并进行单个 json 响应?
我的方法正确吗?还有其他更简单的方法吗?
N.B:我的管理仪表板在后端,所以我使用 ejs 作为模板引擎
这很简单。您所要做的就是按照格式将 public_id 附加到 cloudinary 的 URL 中。
这是上传图片后来自 cloudinary 的示例响应(我省略了很多字段以保持答案准确):-
{
"public_id": "eneivicys42bq5f2jpn2",
"version": 1570979139,
"width": 1000,
"height": 672,
"format": "jpg",
"resource_type": "image",
"type": "upload",
"placeholder": false,
"url": "http://res.cloudinary.com/demo/image/upload/v1570979139/eneivicys42bq5f2jpn2.jpg",
"secure_url": "https://res.cloudinary.com/demo/image/upload/v1570979139/eneivicys42bq5f2jpn2.jpg",
"access_mode": "public",
"original_filename": "sample",
}
完整样本响应 here
如您所见,public_id 是图像 URL 中的最后一个参数(在 URL 和 secure_url 字段中给出)以及其他参数,例如版本、图片格式、云名称等
总之:-
您可以使用版本、public_id、云名称等构建完整的 URL,或者仅将响应中的 secure_url 存储在您的 JSON 文档中.
您所要做的就是向客户端发送相同的响应并在那里获取 image/file。您可以向管理员提供 link 本身或作为 <img src={url_of_file} />