Home > Blockchain >  node js and mongoose invalid arg type error
node js and mongoose invalid arg type error

Time:09-01

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. 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

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)

CodePudding user response:

Find user beforehand and then pass the data you want to the 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"})
            }
           
  };
  • Related