Adding data in MongoDB via NodeJS/Webix

383 views Asked by At

It is my first post on Stack Overflow and I hope to be helped like I can help somebody if needed. I am trying to do my own DB with an user friendly interface for Add/Edit/Del values in my datatable.

I can Edit/Remove data from my MongoDB but I can't Add anything to it from my datable in my html page.

If you can see something wrong (right now I am lost!): I guess my problem is localized in my Adding part because when I try to add something in my db from the UI I have the BLABLABLA error message in my terminal.

var express = require('express');
var path = require('path');
var bodyParser = require('body-parser');

//connect to the mongo
var db = require('mongoskin').db("mongodb://localhost/TESTING", { w: 0});
    db.bind('test2test');

//create the app instance
var app = express();
//serve static files
app.use(express.static(path.join(__dirname, 'public')));
//parse POST data
app.use(express.json());
app.use(express.urlencoded());

//response for saving operations
function after_update(err, res, test2test){
    if (err){
        res.status(500);
        res.send({ error:err.toString() });
    } else {
        res.send(record || {});
    }
}

//data loading
app.get('/data', function(req, res){
    db.test2test.find().toArray(function(err, data){
        for (var i = 0; i < data.length; i++){
            //map _id to id
            data[i].id = data[i]._id;
            delete data[i]._id;
        }
        res.send(data);
    });
});

//adding
app.post('/data', function(req, res){
    db.test2test.insert(req.body, function(err, test2test){
        if (err) {
            console.log("BLABLABLA");
            return res.send({ status:"error" });
        }
        res.send({ newid:req.body._id });
    });
});

//updating
app.put('/data/:id', function(req, res){
    db.test2test.updateById(req.param("id"), req.body, function(err){
        if (err) return res.send({ status:"error" });
        res.send({});
    });
});

//deleting
app.delete('/data/:id', function(req, res){
    db.test2test.removeById(req.param("id"), req.body, function(err){
        if (err) return res.send({ status:"error" });
        res.send({});
    });
});

app.listen(3000);

UPDATED

New problem guys, when I try to add data I have this message via console.log(err) :

[MongoError: driver is incompatible with this server version] name: 'MongoError'

When I run Mongo from my shell I can see :

MongoDB shell version v3.4.0
connecting to: mongodb://127.0.0.1:27017
MongoDB server version: 3.2.11
WARNING: shell and server versions do not match

I use :

MongoDB 3.4
Express 3.5.3
Mongoskin ~1.3.20

And of course I always can not add anything to my db..


FINALLY

I just updated Mongoskin from 1.3.20 to the last version 2.1.0 and everything works fine )) so happy

Thanks a lot everybody for your help

1

There are 1 answers

1
Sourbh Gupta On

may be your error is due to POST call didn't find value in req.body. You have required the body-parser but didn't use it.

app.use(express.urlencoded());

instead of this use :

app.use(bodyParser.urlencoded());