【问题标题】:How to retrieve array values from success callback in Jquery如何从 Jquery 中的成功回调中检索数组值
【发布时间】:2019-07-09 16:36:01
【问题描述】:

我想从 nodejs 获取数组数据。特别是我期待获得我使用 Jquery 检索数据但我做不到的每个结果的文件名。我不知道该怎么做。

router.post('/womensearch',(req,res)=>{
const searchItem = req.body.womenitem;
// If it is Catagory
gfs.collection('wdress').find({
    $and:[{ $or : [{'metadata.cat': searchItem},{'metadata.brand': 
searchItem}]}]
   }).toArray((err,files)=>{

    res.end(JSON.stringify(files)); <- Here is my problem

  });
});

这是 Jquery 代码

      function womenSearch(){
          $(document).ready(function(){
            const search_item = $('#womenItem').val();
            $.ajax({
              method:'POST',
              url:'/womensearch',
              cache:false,
              data:{
                womenitem:search_item
              },
              success:function(data){


              },
              error:function(){

              }
            });

          });
        }


        </script>

这是我从数据库中得到的结果。

{ _id: 5c62dfbf87dda7249c87c190,
 length: 129450,
 chunkSize: 261120,
  uploadDate: 2019-02-12T15:01:19.617Z,
  filename: 'd03e3359c03a5d885e85d6a48c6424a5.jpg',
  md5: '88f70c2f16f1367fe7198775a3912daa',
  contentType: 'image/jpeg',
  metadata:
  { brand: 'Mango',
   cat: 'Coats',
   name: 'Harish',
   color: 'Blue',

   price: '2750',
   info: 'Stylish gesture for women ',
   dis: '50',
   Likes: 2 } },
 { _id: 5c62e3d20a7a574dbc7c0044,
 length: 150261,
 chunkSize: 261120,
 uploadDate: 2019-02-12T15:18:42.036Z,
 filename: 'eb1528f1e7e9707f2d79a6d97981a9c8.jpg',
 md5: '08c1bcd73521e00f6b4857363f950e52',
contentType: 'image/jpeg',

metadata:
 { brand: 'Mango',
   cat: 'Coats',
   name: 'Harish',
   color: 'Blue',
   price: '5000',
   info: 'This is wonderful ',
   dis: '0',
   Likes: 0 } } ]

通过 Jquery 回调函数发送数据的任何提示。

【问题讨论】:

  • 当你的log response 成功时你会得到什么?
  • 如果我在成功博客中提到 data 我会得到一个 Json 文件的整个结果但是如果我提到 data.filename 我会得到未定义
  • 正是我不知道如何通过成功博客获取数组结果

标签: jquery node.js mongodb


【解决方案1】:

您需要在访问之前将结果解析为json

resultJSON = JSON.parse(data);

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-04-23
    • 1970-01-01
    • 2012-08-25
    • 2011-10-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多