Node.js: req.query [] और req.params के बीच अंतर


124

क्या QUERY_STRING तर्कों को प्राप्त करने req.query[myParam]और के बीच अंतर है req.params.myParam? यदि हां, तो मुझे कब उपयोग करना चाहिए?

जवाबों:


142

req.paramsमार्ग पैरामीटर (URL के पथ भाग में) req.queryशामिल हैं , और URL क्वेरी पैरामीटर (URL के बाद ?) शामिल हैं।

आप req.param(name)दोनों स्थानों (साथ ही req.body) में एक पैरामीटर देखने के लिए उपयोग कर सकते हैं , लेकिन यह विधि अब पदावनत है।


आह, ठीक है, धन्यवाद, इसलिए दोनों एक्सप्रेस द्वारा प्रदान किए गए हैं। और POST डेटा मैं req.body.myParam के माध्यम से एक्सेस करता हूं?

1
सही। कौन सा उपयोग करना है यह इस बात पर निर्भर करता है कि आप क्या करने की कोशिश कर रहे हैं।
जॉनीएचके

यह भी ध्यान दें: "req.body, req.params, और req.query तक सीधी पहुंच स्पष्टता के पक्ष में होनी चाहिए - जब तक कि आप वास्तव में प्रत्येक ऑब्जेक्ट से इनपुट स्वीकार नहीं करते हैं।" - एक्सप्रेस प्रलेखन
रयान क्यू

2
req.paramअब पदावनत हो गया है। नोड का उपयोग कर पता चलता है req.queryयाreq.params
SaiyanGirl

3
इसे क्यों अपवित्र करें? क्या होगा अगर हम params या क्वेरी का उपयोग करते हैं और फिर इसे दूसरे में बदलने का निर्णय लेते हैं?
जॉन

247

इस मार्ग को दिया

app.get('/hi/:param1', function(req,res){} );

और यह URL दिया http://www.google.com/hi/there?qs1=you&qs2=tube

आपके पास होगा:

अनुरोध। सवाल

{
  qs1: 'you',
  qs2: 'tube'
}

अनुरोध। पैरामीटर

{
  param1: 'there'
}

एक्सप्रेस req.params >>


क्या होगा यदि मुझे / hi / पाने की आवश्यकता है?
डैनियल

2
req.url या req.originalUrl या req._originalUrl पर एक नज़र डालें, फिर /
ruffrey

22

मान लीजिए कि आपने अपने मार्ग का नाम इस तरह परिभाषित किया है:

https://localhost:3000/user/:userid

जो बन जाएगा:

https://localhost:3000/user/5896544

यहां, यदि आप प्रिंट करेंगे: request.params

{
userId : 5896544
}

इसलिए

request.params.userId = 5896544

इसलिए request.params नामांकित मार्ग के गुणों वाली एक वस्तु है

और request.query URL में क्वेरी पैरामीटर से आता है जैसे:

https://localhost:3000/user?userId=5896544 

request.query

{

userId: 5896544

}

इसलिए

request.query.userId = 5896544

अच्छी व्याख्या
Abk

7

अब आपको डॉट नोटेशन का उपयोग करके क्वेरी को एक्सेस करने में सक्षम होना चाहिए।

यदि आप यह कहना चाहते हैं कि आपको एक GET अनुरोध प्राप्त हो रहा है /checkEmail?type=email&utm_source=xxxx&email=xxxxx&utm_campaign=XXऔर आप उपयोग की गई क्वेरी को प्राप्त करना चाहते हैं ।

var type = req.query.type,
    email = req.query.email,
    utm = {
     source: req.query.utm_source,
     campaign: req.query.utm_campaign
    };

परम को अनुरोध प्राप्त करने के लिए स्व परिभाषित पैरामीटर के लिए उपयोग किया जाता है, जैसे कुछ (उदाहरण):

router.get('/:userID/food/edit/:foodID', function(req, res){
 //sample GET request at '/xavg234/food/edit/jb3552'

 var userToFind = req.params.userID;//gets xavg234
 var foodToSearch = req.params.foodID;//gets jb3552
 User.findOne({'userid':userToFind}) //dummy code
     .then(function(user){...})
     .catch(function(err){console.log(err)});
});

0

मैं एक महत्वपूर्ण नोट के बारे में उल्लेख करना चाहता हूं req.query, क्योंकि वर्तमान में मैं पृष्ठ पर आधारित कार्यक्षमता पर काम कर रहा हूं req.queryऔर मेरे पास आपके लिए प्रदर्शित करने के लिए एक दिलचस्प उदाहरण है ...

उदाहरण:

// Fetching patients from the database
exports.getPatients = (req, res, next) => {

const pageSize = +req.query.pageSize;
const currentPage = +req.query.currentPage;

const patientQuery = Patient.find();
let fetchedPatients;

// If pageSize and currentPage are not undefined (if they are both set and contain valid values)
if(pageSize && currentPage) {
    /**
     * Construct two different queries 
     * - Fetch all patients 
     * - Adjusted one to only fetch a selected slice of patients for a given page
     */
    patientQuery
        /**
         * This means I will not retrieve all patients I find, but I will skip the first "n" patients
         * For example, if I am on page 2, then I want to skip all patients that were displayed on page 1,
         * 
         * Another example: if I am displaying 7 patients per page , I want to skip 7 items because I am on page 2,
         * so I want to skip (7 * (2 - 1)) => 7 items
         */
        .skip(pageSize * (currentPage - 1))

        /**
         * Narrow dont the amound documents I retreive for the current page
         * Limits the amount of returned documents
         * 
         * For example: If I got 7 items per page, then I want to limit the query to only
         * return 7 items. 
         */
        .limit(pageSize);
}
patientQuery.then(documents => {
    res.status(200).json({
        message: 'Patients fetched successfully',
        patients: documents
    });
  });
};

आप के +सामने हस्ताक्षर देखा जाएगा req.query.pageSizeऔरreq.query.currentPage

क्यों? यदि आप हटाते हैं+ इस मामले में , तो आपको एक त्रुटि मिलेगी, और उस त्रुटि को फेंक दिया जाएगा क्योंकि हम अमान्य प्रकार (त्रुटि संदेश के साथ 'सीमा' फ़ील्ड संख्यात्मक होना चाहिए) का उपयोग करेंगे।

जरूरी : डिफ़ॉल्ट रूप से यदि आप इन क्वेरी मापदंडों से कुछ निकालते हैं, तो यह हमेशा एक स्ट्रिंग होगा , क्योंकि यह URL आ रहा है और इसे एक पाठ के रूप में माना जाता है।

अगर हमें संख्याओं के साथ काम करना है, और क्वेरी स्टेटमेंट को टेक्स्ट से नंबर में बदलना है, तो हम केवल स्टेटमेंट के सामने एक प्लस साइन जोड़ सकते हैं।

हमारी साइट का प्रयोग करके, आप स्वीकार करते हैं कि आपने हमारी Cookie Policy और निजता नीति को पढ़ और समझा लिया है।
Licensed under cc by-sa 3.0 with attribution required.