0

Trying to save the checked Genre checkbox data into database. But, it's not saving data.. .. instead it is saving an empty array for genre.

Tried to send data by following this thread, this thread. None of them, worked for me

I have attached create controller and form for that.

BookController create:

exports.bookCreate = [
    (req, res, next) => {
        if(!(req.body.genre instanceof Array)){
            if(typeof req.body.genre==='undefined')
            req.body.genre=[];
            else
            req.body.genre=new Array(req.body.genre);
        }
        next();
    },
    async(req, res, next) => {
            const errors = validationResult(req);

            const book = await new Book({
                title   : req.body.title,
                genre   : req.body.genre,
            });

            if (!errors.isEmpty()) {
                async.parallel({
                    genres: (callback) => {
                        Genre.find(callback);
                    },
                }, (error, values) => {
                    if(error) return next(error);

                    // Mark Selected Genres as Checked
                    for (let i = 0; i < values.genres.length; i++) {
                        if (book.genre.indexOf(values.genres[i]._id) > -1) {
                            values.genres[i].checked='true';
                        }
                    }

                    res.render('./book/create', {
                        title   : 'Create Book',
                        genres  : values.genres,
                        book    : book,
                        errors  : errors.array()
                    });
                });

                return;

            } else {
               book.save((error) => {
                  if(error) return next(error);
                     res.redirect('./');
               });

            }
        }
];

create.ejs:

if errors
        .alert.alert-danger(role='alert')
            ul
                for error in errors
                    li!= error.msg 

    form(method='POST' action='')

        div.form-group
            label(for='title') Title:
            input#title.form-control(type='text', placeholder='Name of Book' name='title' value=(undefined===book ? '' : book.title) )

        div.form-group
            label Genre:
            div
                for genre in genres
                    div(style='display: inline; padding-right:10px;')
                        input.checkbox-input(type='checkbox', name='genre', id=genre._id, value=genre._id, checked=genre.checked)
                        label(for=genre._id) #{genre.name}

        button.btn.btn-primary(type='submit') Submit

Any help would be appreciated.

tanjiya
  • 173
  • 3
  • 16
  • I found a couple of strange things in code: 1. `req.body.genre=new Array(req.body.genre);` It can return the array of array [req.body.genre] 2. `Genre.find` looks like the method to find by id (or some search query), but you don't pass an *search query* – viktarpunko Nov 01 '19 at 10:06
  • @viktarpunko, okay. 1. The first instance return an array. as i have put if(!(req.body.genre instanceof Array)) and, if there is only one instance, it will also be returned as array. 2. genre.find is going to retrive all of the genre related to the book. and that works fine. ... .. and here is the reference [msdn](https://developer.mozilla.org/en-US/docs/Learn/Server-side/Express_Nodejs/forms/Create_book_form) – tanjiya Nov 01 '19 at 10:30
  • Add an information aboud `Book` class. I think the issue inside `book.save` method. – viktarpunko Nov 01 '19 at 10:41
  • @viktarpunko Okay. can you please suggest me what to do? cause, the other data are storing without the genre (the checked data). – tanjiya Nov 01 '19 at 10:48
  • the issue might be in `book.save(error)` – viktarpunko Nov 01 '19 at 10:58
  • @viktarpunko, if there would have an issue with save method, then the data will not be saved. is not it? but, then data is saving. though, i have removed the callback and the result is same. – tanjiya Nov 01 '19 at 11:01

0 Answers0