我正在嘗試使用引數通過用戶名查找用戶。這是我的服務器端。
const express = require("express");
const mongoose = require("mongoose");
const app = express();
//ROUTES
const userRoute =require("./src/routes/userRoutes");
const postRoute =require("./src/routes/postRoutes");
const commentRoute =require("./src/routes/commentRoutes");
const port = process.env.PORT || 5000;
mongoose.connect("mongodb://localhost:27017/projectDB", {useNewUrlParser: true});
app.use(express.json());
app.use(express.urlencoded());
app.get("/", (req, res) =>{
res.send("hello");
});
app.use("/", userRoute, postRoute, commentRoute);
app.listen(port, () => console.log(`Example app listening at http://localhost:${port}`));
這是我的用戶路由代碼。
const express = require('express');
const userRouter = express.Router();
const userController = require("../controllers/userController");
userRouter.route("/users/:userName")
.get(userController.getUser)
.patch(userController.updateUser)
.delete(userController.deleteUser)
.post(userController.newUser);
module.exports = userRouter;
這是用戶的控制器。一切都非常簡單。
const mongoose = require("mongoose");
const Users = require("../models/userModel");
module.exports.getUser = (req, res) => {
console.log(req.params.userName);
res.status(200).send(Users.find({name: req.params.userName}), (err, res)=>{
if (res.err){
console.log("USER NOT FOUND");
}else{
console.log("Success!");
}
});
};
當我嘗試獲取請求以路由“..../users/gokdeniz”這是資料庫中的有效用戶時,我收到一條錯誤訊息
TypeError [ERR_INVALID_ARG_TYPE]: The first argument must be of type string or an instance of Buffer, ArrayBuffer, or Array or an Array-like Object. Received type function ([Function (anonymous)])
at new NodeError (node:internal/errors:371:5)
at Function.from (node:buffer:322:9)
at ServerResponse.send (C:\Users\tlege\OneDrive\Masaüstü\project 1\untitled_project\Server\node_modules\express\lib\response.js:193:22)
at module.exports.getUser (C:\Users\tlege\OneDrive\Masaüstü\project 1\untitled_project\Server\src\controllers\userController.js:6:21)
at Layer.handle [as handle_request] (C:\Users\tlege\OneDrive\Masaüstü\project 1\untitled_project\Server\node_modules\express\lib\router\layer.js:95:5)
at next (C:\Users\tlege\OneDrive\Masaüstü\project 1\untitled_project\Server\node_modules\express\lib\router\route.js:144:13)
at Route.dispatch (C:\Users\tlege\OneDrive\Masaüstü\project 1\untitled_project\Server\node_modules\express\lib\router\route.js:114:3)
at Layer.handle [as handle_request] (C:\Users\tlege\OneDrive\Masaüstü\project 1\untitled_project\Server\node_modules\express\lib\router\layer.js:95:5)
at C:\Users\tlege\OneDrive\Masaüstü\project 1\untitled_project\Server\node_modules\express\lib\router\index.js:284:15
at param (C:\Users\tlege\OneDrive\Masaüstü\project 1\untitled_project\Server\node_modules\express\lib\router\index.js:365:14)
uj5u.com熱心網友回復:
事先找到用戶,然后將您想要的資料傳遞給 send() 函式。
module.exports.getUser = async (req, res) => {
const user = await Users.find({name: req.params.userName});
if (user) {
res.status(200).send(user)
} else {
res.status(404).send({error: "Not found"})
}
};
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/504002.html
標籤:javascript 节点.js 猫鼬 后端
