简体   繁体   中英

Cant target value in Dictionary Node.js

const j = require('request').jar();
const request = require('request').defaults({
  jar: j
});

request({
  method: 'GET',
  url: "https://www.google.com/gen_204?atyp=csi&ei=_-34WouCCoaWsgWUsLfoBQ&s=newtab&adh=&conn=onchange&ima=1&ime=0&imeb=0&imeo=0&mem=ujhs.10,tjhs.10,jhsl.2190&rt=aft.13,ol.20,prt.14,xjs.317,xjsee.317,xjses.230,xjsls.37,wsrt.95,cst.0,dnst.0,rqst.18,rspt.10,rqstt.27,unt.25,cstt.25,dit.111&zx=1526264059738"
}, function(err, res, body) {
  console.log(JSON.stringify(j._jar.cookies));

this gives:

undefined

  console.log(JSON.stringify(j));

this gives:

{"_jar":{"version":"tough-cookie@2.3.3","storeType":"MemoryCookieStore","rejectPublicSuffixes":true,"cookies":[{"key":"NID","value":"130=F2nMvgnjWwGNndAE7c0rlU8AIgvX-EsDL-qc_kFLT18upWh87niN-_Th5oMbUyDe7LV5HWNVcsUKb6SUwQuC6VTWgC-jnPmHo4tLnFPb8r7p3CEbMN7MYxAFpucRfl8o","expires":"2018-11-13T03:50:35.000Z","domain":"google.com","path":"/","httpOnly":true,"hostOnly":false,"creation":"2018-05-14T03:50:35.944Z","lastAccessed":"2018-05-14T03:50:35.944Z"}]}}

});

I need the cookies:[] as shown in the output above.

j is an instance of RequestJar Object while j._jar is an instance of CookieJar Object. CookieJar(_jar) does not have cookies field but you can access JSON.parse(JSON.stringify(j._jar)).cookies

request({
  method: 'GET',
  url: "https://www.google.com/gen_204?atyp=csi&ei=_-34WouCCoaWsgWUsLfoBQ&s=newtab&adh=&conn=onchange&ima=1&ime=0&imeb=0&imeo=0&mem=ujhs.10,tjhs.10,jhsl.2190&rt=aft.13,ol.20,prt.14,xjs.317,xjsee.317,xjses.230,xjsls.37,wsrt.95,cst.0,dnst.0,rqst.18,rspt.10,rqstt.27,unt.25,cstt.25,dit.111&zx=1526264059738"
}, function(err, res, body) {
    console.log(JSON.parse(JSON.stringify(j._jar)).cookies);
  });

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM