我有這個用戶路線
const express = require('express');
const router = express.Router();
const {
GetAll,
} = require('../areas/directory/controllers/usercontroller');
router.route('/getall?:username&:email').get(GetAll);
module.exports = router;
當我嘗試像這樣訪問 Postman 中的 url 時: http://localhost:5000/api/user/getall?username=nameone&email=emailone
我收到此錯誤:無法獲取 /api/user/getall
但是,如果我將其更改為
router.route('/getall/:username&:email').get(GetAll);
并像這樣訪問 Postman 中的 url:http://localhost:5000/api/user/getall/username=nameone&email=emailone
有用。
另一方面,即使它有效,我也無法獲得變數的值。
var username = req.params.username;
將回傳 username=nameone 。
uj5u.com熱心網友回復:
為了http://localhost:5000/api/user/getall?username=nameone&email=emailone作業,你應該改變
router.route('/getall?:username&:email').get(GetAll);
到
"router.route('/getall').get(GetAll);"
并用于req.query.username訪問用戶名查詢引數req.query.email的值和訪問電子郵件查詢引數的值。
進行這些更改后,您可以http://localhost:5000/api/user/getall?username=nameone&email=emailone在 Postman 中呼叫,您將能夠在您的代碼中看到usernameand的值。email
這是因為您不必在路徑中指定查詢引數,例如?:username在router.route('getall').
編輯:添加更多有關路徑和查詢的詳細資訊
請參閱此問題的前 2 個解決方案,以了解有關路徑和查詢的更多資訊,以及為什么應將代碼更改為我上面提到的方式:這里是鏈接。
uj5u.com熱心網友回復:
錯誤原因...
實際上,您正在嘗試做的事情稱為在其余 api 中傳遞的查詢。
但是您正試影像引數一樣訪問它們
解決方案??
按照步驟訪問查詢
**使用?傳遞資料?在 api 請求中應該是這樣的 你可以有多個查詢物件
http://localhost:5000/getall?email='email'&&password='password'
像這樣訪問快遞應用程式中的查詢
app.post('/getall', (req, res) => {
const {
email,
password
} = req.query
return res.status(200).json({
email,
password
})
})
req.query包含您嘗試傳遞的資料throw ?然后查詢屬性
上面代碼的回應是這樣的
{
"email": "'email'",
"password": "'password'"
}
uj5u.com熱心網友回復:
您不應在 URL 中提供查詢引數。因此,為了表達,url 必須是公正的/getall,并且在您的代碼中,您可以使用訪問這些變數req.params.whatever_query_parameter
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/432983.html
標籤:javascript 节点.js 表示
