H
H
Hiroshima2021-03-27 16:45:08
Node.js
Hiroshima, 2021-03-27 16:45:08

Why does it throw an error User.every is not a function?

I want to extract the username from this object into the usernameActive variable :

{ owner: { name: 'Jack', password: '12345' },
  _id: 605f27e660d71e175c928c68,
  __v: 0 }


Here is what I do:
var express = require('express');
var router = express.Router();
var Schedule = require("../models/schedule");
var ChatList = require("../models/chatList");
var usernameActive;

router.param("owner", async function(req, res, next, owner) {
    try {
        const User = await Schedule.findOne({ "owner.name": owner });
        User.every(function(keyUsername) {
            usernameActive = keyUsername;
        });
        console.log(usernameActive);

        if (User) {
            req.owner = User;
            next();
        } else {
            next(createError(403));
        }

    } catch (error) {
        console.log('error')
        next(error);
    }
});

/* GET users listing. */
router.get("/:owner", function(req, res, next) {
    res.render("user", { user: req.owner });
});

module.exports = router;


Here's what actually happens:
User.every is not a function
TypeError: User.every is not a function
at H:\Autorization\routes\users.js:10:14
at process._tickCallback (internal/process/next_tick.js :68:7)

Answer the question

In order to leave comments, you need to log in

1 answer(s)
I
Igor, 2021-03-27
@hurgadan

Obviously because your User is null. User.findOne did not find a user by the specified condition, so User is null

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question