H
H
Hiroshima2021-03-13 13:36:22
Mongoose
Hiroshima, 2021-03-13 13:36:22

How to extract a parameter from a model object?

Good day, I can’t extract name from owner into a variable, and then display the variable in the console.
Model:

const { Schema, model } = require('mongoose');
const scheduleSchema = new Schema({
    owner: {
        name: {
            type: String,
            required: true
        },
        password: String
    },
    schedule: [{
        dow: {
            type: String,
            required: true
        },
        lessons: [{
            start: String,
            name: String
        }]
    }]
});
module.exports = model('Schedule', scheduleSchema);


The code that, in my opinion, should display the names in the console, but in the end it displays an empty array, although there is data in mongodb, and they function successfully when the user registers:
var express = require("express");
var router = express.Router();
var Schedule = require("../models/schedule");

router.post("/", async function(req, res, next) {
    const isPasswordConfirmed = req.body.password === req.body.password_repeat;
    if (req.body.username && isPasswordConfirmed) {

        var info = await Schedule.find({ "owner.name": [] }); // <---  ВОТ В ЭТОЙ ШТУКЕ ВСЯ МОЯ ПРОБЛЕМА
        console.log(info)

        // Schedule.find()
        //     // .sort([
        //     //     ['owner.name']
        //     // ])
        //     .exec(function(err, list_name) {
        //         if (err) { return next(err); }
        //         console.log(list_name);
        //     })

        const newUser = new Schedule({
            owner: {
                name: req.body.username,
                password: req.body.password
            }
        });
        await newUser.save();
        res.redirect(`/user/${newUser.owner.name}`);
    } else {
        console.log('Отсечка')
    }
    res.render("register", { error: { message: "Пароли не совпадают" } });
});
router.get("/", function(req, res, next) {
    res.render("register");
});

module.exports = router;

Answer the question

In order to leave comments, you need to log in

1 answer(s)
I
Ivan, 2021-03-13
@Hiroshima

Maybe
Schedule.find({ "owner.name": {$eq: 'someone' })
And according to the scheme you have Onwer.name string and in the code you compare it with an empty array ..

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question