1

I am making an app where the app is going to send the POST request data to the nodeJS server. The JSON format of the content looks like: {"encrypteddata": "someencryptedvalueofthetext"}. This data will be saved in a MongoDB.
I created two file one is app.js and another is /models/encdata.js. Content of both the file is given below.
app.js

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

app.use(bodyParser.json());

ENCDATA = require('./models/encdata');

mongoose.connect('mongodb://localhost/encdata', { useMongoClient: true }); // the url access the database

var db = mongoose.connection;

app.get('/', function(req, res){

    res.send('Visit /api/encdata');

    app.get('/api/encdata', function(req, res){
    ENCDATA.getENCDATA(function(err, encdata){
            if(err){
                throw err;
            }
            res.json(encdata);
        });
    });

    app.post('/api/encdata', function(req, res){
        var encdata = req.body;
        ENCDATA.addENCDATA(encdata, function(err, encdata){
            if(err){
                throw err;
            }
            res.json(encdata);
        });
    });

 });

 app.listen(3000);
 console.log('Running on port 3000');

encdata.js

var mongoose = require('mongoose');

var encdataencryptSchema = mongoose.Schema({
    encrypteddata: {
        type: String,
        required: true
    }
});

var ENCDATA = module.exports = mongoose.model('encdata', encdataencryptSchema);

module.exports.getENCDATA = function(callback, limit){
    ENCDATA.find(callback).limit(limit);
}

module.exports.addENCDATA = function(encdata, callback){
    ENCDATA.create(encdata, callback);
}

And data in MongoDB is:

{"encrypteddata": "someencryptedvalueofthetext"}

But when I make a GET request to the url localhost:3000/api/encdata it shows [] (an empty array although I have data). Even the POST request is not working and I am not able to save any data.

13
  • You are not closing these braces: app.get('/', function(req, res) { Commented Nov 2, 2017 at 14:50
  • I am closing that and I am not getting any typo error like that. Commented Nov 2, 2017 at 14:52
  • But you are closing it after declare the other endpoints... Commented Nov 2, 2017 at 14:53
  • I tried that too and the problem is the same. However what I did is also gonna work. Commented Nov 2, 2017 at 14:59
  • Have you tried passing a limit to ENCDATA.getENCDATA() method? Commented Nov 2, 2017 at 15:03

1 Answer 1

1

I rewrote your code by changing the name of the variable and it worked for me. The app.js file looks something like this:

var express = require('express');
var app = express();
var bodyParser = require('body-parser');
var mongoose = require('mongoose');
var http = require('http');

app.use(bodyParser.json());

AES = require('./models/aes');

mongoose.connect('mongodb://localhost/aes', { useMongoClient: true }); //     the url access the database

var db = mongoose.connection;

app.get('/', function(req, res){

res.send('Visit /api/aes');

 app.get('/api/aes', function(req, res){
      AES.getAES(function(err, aes){
         if(err){
             throw err;
         }
         res.json(aes);
     });
 });

    app.post('/api/aes', function(req, res){
        var aes = req.body;
         AES.addAES(aes, function(err, aes){
             if(err){
                throw err;
            }
            res.json(aes);
        });
    });

});



app.listen(3000);
console.log('Running on port 3000');

In the encdata.js you can change the variable to AES. Name the mongodb collection and database as aes.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks! This worked for me. But I cannot understand what was the problem in my code. Just by changing the variable how this can work.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.