Wednesday, 31 May 2017

node express mongodb add data to database

I need add word to database if that word not exist in my database, if exist show message "word arleady exist, enter another word". Now my code add new word or update if word exist, but i cant to do alert message to show that word arleady exist

router.post('/insert', function (req, res, next) {
    if (req.body.title.length == 0) {
        var inputFile = "./k5audios/" + req.body.inputFile;
        var item = {
            title: req.body.inputFile.substring(0, (req.body.inputFile.length - 4)),
            content: inputFile
        }
    } else {
        var item = {
            title: req.body.title.toLowerCase(),
            content: "https://ssl.gstatic.com/dictionary/static/sounds/de/0/" + req.body.title.toLowerCase() + ".mp3"
        }
    }

    mongo.connect(url, function (err, db) {
        assert.equal(null, err);
       db.collection('words').update(item,
           {$inc: {score: 1}},
           {upsert: true, safe: false},
           function(err,data){
               if (err){
                   console.log(err);
               }else{
                   console.log("score succeded");
               }
           });
    });
});
<form action="/insert" method="post">
                                <div class="modal-body">
                                    <div class="form-group">
                                        <label for="title">New Word</label>
                                        <input type="text" class="form-control" id="title" name="title" placeholder="Enter new word">
                                    </div>
                                    <div class="form-group">
                                        <label for="exampleInputFile">File input</label>
                                        <input type="file" id="inputFile" name="inputFile">
                                    </div>
                                </div>
                                <div class="modal-footer">
                                    <button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
                                    <button type="submit" class="btn btn-primary">Save changes</button>

                                </div>

                            </form>

enter image description here



via Max Ross

No comments:

Post a Comment