将JSON解析为Array for Cloudinary和node.js

问题描述 投票:0回答:1

我正在尝试使用返回的JSON对象创建一个Array。我正在使用Cloudinary.openUploadWidget的回调并解析“成功”的响应。我发现它正在添加来自服务器的每个响应,所以我最终得到了同一图像的数组中的多个项目。我只需要每个图像一个项目..

我已经能够将这些解析为数组,但每次返回都会创建多个数组。我只需要一个成功上传图像的数组,就像旧版本一样。

这是我的代码,请帮忙!

theThum = [];

// Upload button 
$uploadBtn.click(function() { 

cloudinary.openUploadWidget({ 
cloud_name: 'xxxxx', 
upload_preset: 'xxxxxx', 
sources: [ 'local', 'url', 'camera', 'image_search', 'facebook', 'google_photos' ], // 'dropbox' 
// dropbox_app_key: 'xxxxx', 
google_api_key: 'xxxxxx', 
search_by_sites: ["all", "cloudinary.com"], 
search_by_rights: true 
}, 
(error, result) => {

//result = Array.from(result); 
if(result.event === 'success') {    
theThum.push(result.info);  
console.log(theThum); 
setThumb(theThum);

};  

});

});

//console.log(myResult); 
//console.log(myResult); 
function setThumb(myResult) { 

myResult.map(function(img_data, idx) { 
var $files = []; 
var $placeholder = renderPlaceholder();

$placeholder.prop('data-id', img_data.public_id); 
uploads[img_data.public_id] = img_data; 
processUploads(); 

if (imagePreviews) { 
$placeholder.find('.img-thumbnail .placeholder').prop('src', img_data.url).prop( 'title', escape(img_data.public_id) ); 
} 

$placeholder.find('.btn-undo-upload').click(function() { 
if (directUploading) { 
if ($placeholder.prop('data-id')) { 
delete uploads[$placeholder.prop('data-id')]; 
processUploads(); 
} 
} else { 
$field.remove(); 
} 

$placeholder.remove(); 

checkQueues(); 
}); 

$files.push(_.first($placeholder)); 

updateStatus($placeholder, 'Remove', 'glyphicon-ok'); 

checkQueues(); 
$(window).trigger('redraw'); 

return $placeholder; 
})  
};

这是console.log函数中返回的内容

(2) [{…}, {…}]
0: {public_id: "spirit-fest1604170329_copy_rwmfaa", version: 1537141402, signature: "8ac06c23a1da8d138cf28e8c9696acb5cde54dcd", width: 379, height: 506, …}
1: {public_id: "FullSizeRender_gcten2", version: 1537141451, signature: "8de460788d6f1a782accf81c6e18aeb600cef3e3", width: 640, height: 468, …}
2: {public_id: "spirit-fest1604170329_copy_ds54m9", version: 1537141451, signature: "60baed9f0ce892779e09c124b0d22fc8987bc1c4", width: 379, height: 506, …}
length: 3
__proto__: Array(0)
ui-cloudinaryimages.js:232 
(3) [{…}, {…}, {…}]
0: {public_id: "spirit-fest1604170329_copy_rwmfaa", version: 1537141402, signature: "8ac06c23a1da8d138cf28e8c9696acb5cde54dcd", width: 379, height: 506, …}
1: {public_id: "FullSizeRender_gcten2", version: 1537141451, signature: "8de460788d6f1a782accf81c6e18aeb600cef3e3", width: 640, height: 468, …}
2: {public_id: "spirit-fest1604170329_copy_ds54m9", version: 1537141451, signature: "60baed9f0ce892779e09c124b0d22fc8987bc1c4", width: 379, height: 506, …}
length: 3
__proto__: Array(0)
arrays json cloudinary
1个回答
0
投票

结果以数组的形式在响应中包含所有上载信息。如果您查看result.info,您将找到与先前版本上载响应相同的响应结构。例如,result.info.public_id将返回公共ID

© www.soinside.com 2019 - 2024. All rights reserved.