[英]node js and mongoose invalid arg type error
I am trying to find a user by their username using parameters.我正在尝试使用参数通过用户名查找用户。 Here is my server side.
这是我的服务器端。
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}`));
here is my routing code for user.这是我的用户路由代码。
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;
and here is the controller for the user.这是用户的 controller。 All very simple.
一切都非常简单。
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!");
}
});
};
when I try a get request to route "..../users/gokdeniz" which is a valid user in the database, I get an error saying当我尝试获取请求以路由“..../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)
Find user beforehand and then pass the data you want to the send() function.事先找到用户,然后将您想要的数据传递给 send() function。
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"})
}
};
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.