There's no respone for below save method. I wonder why.
app.post('/something', function (req, res) {
mainModel.save(req.body.data ,function(err,data){
if(err){
console.log(err)
}else{
console.log(data)
}
});
});
Here's how my models/main.js look like http://pastebin.com/eQSFXWb5
You didn't return any response after save. you should return response ie. return res.status(400).send().
you can try it
app.post('/something', function (req, res) {
if(!req.body.data) {
return res.status(400).send({masg: 'Invalid data'});
}
var newMainModel = new mainModel(req.body.data);
newMainModel.save(function(err,data){
if(err){
console.log(err)
return res.status(400).send({masg: 'Error occurred'});
}
console.log(data);
return res.status(200).send(data);
});
});
Related
im using express-session with a mongo-store.
However, in my authentication process i call req.session.save() after the user is authenticated successfully. But when i try to send a response to the client i get the message Error [ERR_HTTP_HEADERS_SENT]: Cannot set headers after they are sent to the client
When i send the response before i call req.session.save() and not inside the callback, its working.
This is what my code snippet looks like.
// res.json({ data: 'hello world' }); //This is working...
req.session.save((err) => {
if (err) {
console.log('Error saving session: ', err);
} else {
// res.json({ data: 'Hello CB' }); //Headers already sent
}
});
I also can see that the session is saved in my mongoDb.
I couldn't find anything which tells me, that req.session.save() is sending some response to the client.
Why are the headers already sent in the callback function of req.session.save() ?
Updated full Requestfunction:
const Create = async (req, res) => {
const { code } = req.body;
await Authenticate(code)
.then((user) => {
//Check if User already exists in database
UserModel.getUser(user.id)
.then((resDB) => {
if (resDB === null) {
console.log('User not found');
//User dont exists, create it
UserModel.createUser({
...
})
.then((is_saved) => {
if (is_saved) {
res.json(user);
res.end();
}
})
} else {
req.session.regenerate((err) => {
req.session.value = "somevalue";
});
req.session.save((err) => {
if (err) {
console.log('Error saving session: ', err);
} else {
// res.json({ cookie: req.session });
res.json({ data: 'Hello from CB' });
res.end();
}
});
}
})
.catch((err) => {
console.log('User Create or Login error: ', err);
res.end();
})
})
.catch((err) => {
console.log('Authentication error', err);
res.end();
});
}
Seems like you are missing async and await. Can you share complete method code?
const saveInSession = async (req, res, next) => {
const getData = await req.body; //just an example
req.session.save((err) => {
if (err) {
console.log('Error saving session: ', err);
} else {
// res.json({ data: 'Hello CB' }); //Headers already sent
}
});
};
I'm trying test my node app ,I am able to get post,get and put to work but I can't get delete to work I keep on getting the following error "Type.Error app.address is not a function"
This is my test class :
var supertest = require("supertest");
var should = require("should");
// This agent refers to PORT where program is runninng.
var server = supertest.agent("http://localhost:8000");
// UNIT test begin
describe("Contacts GET unit test",function(){
// #1 should return contacts representation in json
it("should return collection of JSON documents",function(done){
// calling home page api
server
.get("/api/contacts/")
.expect("Content-type",/json/)
.expect(200) // THis is HTTP response
.end(function(err,res){
// HTTP status should be 200
res.status.should.equal(200);
done();
});
});
it("should add a new contact",function(done){
// post to /api/contacts
server.post('/api/contacts')
.send({name:"Contact 99"})
.expect("Content-type",/json/)
.expect(201)
.end(function(err,res){
res.status.should.equal(201);
done();
});
});
it("should update contact",function(done){
// post to /api/contacts
// calling home page api
server.put("/api/contacts/58e275d0aef2392e10eaba6e")
.expect("Content-type",/json/)
.expect(200) // THis is HTTP response
.end(function(err,res){
res.body._id.should.equal("58e275d0aef2392e10eaba6e");
done();
}
);
}
);
it("should delete contact",function(done){
// post to /api/contacts
// calling home page api
const superserver = supertest(server);
server.get("/api/contacts")
.expect("Content-type",/json/)
.expect(200) // THis is HTTP response
.end(function(err,res){
const id = res.body[0]._id;
server.delete("/api/contacts/"+id)
.expect("Content-type",/json/)
.expect(200) // THis is HTTP response
.end(function(err,res){
res.body._id.should.equal(id);
res.body.should.have.property("name");
done();
}
);
}
);
});
});
and this my main class with all the functionalities :
'use strict';
var _ = require('lodash');
var Contact = require('./contact.model');
// Get list of contacts
exports.index = function(req, res) {
// Connect to the db
Contact.find(function (err, contacts) {
if(err) { return handleError(res, err); }
return res.json(200, contacts);
});
} ;
// Creates a new contact in datastore.
exports.create = function(req, res) {
Contact.create(req.body, function(err, contact) {
if(err) { return handleError(res, err); }
return res.json(201, contact);
});
};
// Updates an existing contact in the DB.
exports.update = function(req, res) {
if(req.body._id) { delete req.body._id; }
Contact.findById(req.params.id, function (err, contact) {
if (err) { return handleError(res, err); }
if(!contact) { return res.send(404); }
var updated = _.merge(contact, req.body);
updated.save(function (err) {
if (err) { return handleError(res, err); }
return res.json(200, contact);
});
});
};
// delete an existing contact in datastore.
exports.delete = function(req, res) {
Contact.findById(req.params.id, function (err, contact) {
if(err) { return handleError(res, err); }
if(!contact) { return res.send(404); }
contact.remove(function(err) {
if(err) { return handleError(res, err); }
return res.send(201);
});
});
};
function handleError(res, err) {
return res.send(500, err);
};
This what I get in the command line:
I am not sure if it could help but in your test Class the problem seems to be here (l.62):
const id = res.body[0]._id;
it returns undefined so you get a Type.Error
You should try to debug this line and check the return of :
res.body[0]
PS : api\contacts\test\testContancts.js sure about the name file ?
Hi lets say I have code like this:
router.get('/blablabla', function(req, res, next) {
model
.findOne({condition : req.body.condition})
.then(function(data) {
if(data) {
return res.send("data already exists");
}
else {
//DO CREATE THE NEW DATA
return modelInstance.save();
}
})
.then(function(data) {
if(data) res.send("new data successfully saved");
})
.catch(function(err) {
console.log(err);
});
});
In current condition if data already exists, there will be error
**[Error: Can't set headers after they are sent.]
Error: Can't set headers after they are sent.
at ServerResponse.OutgoingMessage.setHeader (_http_outgoing.js:344:11)**
Why this error raised? based on my knowledge after res.send() is being called then all the later code below it will not be executed right?
I am return data.save() because I want avoid nesting statement.
UPDATE ALTERNATIVE SOLUTION
router.get('/blablabla', function(req, res, next) {
model
.findOne({condition : req.body.condition})
.then(function(data) {
if(data) {
return new Promise(function(resolve, reject) {
return resolve();
});
}
else {
//DO CREATE THE NEW DATA
return modelInstance.save();
}
})
.then(function(data) {
if(data) res.send("new data successfully saved");
else res.send("data already exists");
})
.catch(function(err) {
console.log(err);
});
});
Your second .then() handler will still execute because the promise is not branched, it's chained so the chain keeps going and thus you will be doing two res.send() operations and that's what causes the error you see.
When you do:
return res.send("data already exists");
You are just returning a value from the .then() handler. Since that value is not a rejected promise, the promise stays resolved and the next .then() handler in the chain will execute.
You could do this:
router.get('/blablabla', function (req, res, next) {
model.findOne({condition: req.body.condition}).then(function (data) {
if (data) {
return res.send("data already exists");
} else {
//DO mMODIFY THE DATA AND THEN SAVE THE DATA
return data.save().then(function () {
res.send("new data successfully saved");
});
}
}).catch(function (err) {
console.log(err);
});
});
Or, you could do this (modification of your second try - assumes that data.save() returns a promise that resolves to a value):
router.get('/blablabla', function(req, res, next) {
model
.findOne({condition : req.body.condition})
.then(function(data) {
if (!data) {
//DO mMODIFY THE DATA AND THEN SAVE THE DATA
return data.save();
}
return data;
})
.then(function(data) {
if(data) res.send("new data successfully saved");
else res.send("data already exists");
})
.catch(function(err) {
console.log(err);
});
});
Im new in nodejs, and Im trying to learn by creating an app that has a list of users, that I can add and remove those users. Im using angularjs in frontend to send request to nodejs and after that to mongodb. The problem is that, if I click a lot of times in the button "adduser" a lot of times, my app goes slow.
To interact to mongodb I use:
app.get('/users',function (req, res) {
mongoose.model('Usuario').find(function (err, list) {
res.send(list);
});
});
app.post('/addusuario', function (req,res) {
var usuario = new Usuario(req.body);
usuario.save(function (err) {
if (err) {
console.log(err);
} else {
console.log('Usuario salvo com sucesso');
}
}); });
app.delete('/delusuario/:id', function (req, res) {
var id = req.params.id;
mongoose.model('Usuario').findByIdAndRemove(id , function(err) {
if(err) {
console.log(err);
} else {
console.log('Usuario removido com sucesso!');
}
});
});
Im my angularapp:
app.controller('AppCtrl', function($scope, $http, Data) {
function reload() {
Data.get('users').then(function(data){
$scope.usuarios = data;
console.log(data);
});
};
$scope.addUsuario = function(usuario) {
Data.post('/addusuario', usuario);
reload();
};
$scope.deletarUsuario = function(id) {
Data.delete("/delusuario/"+id).then(function(result) {
});
reload();
};
reload();
});
I dont know why it is becaming slow after I click to add user more than 3 times..
What I see in your code that you are not sending an response back to the user, you should do something after insert or delete in the database. res.end();
You should rewrite your code in the following way:
app.get('/users',function (req, res) {
mongoose.model('Usuario').find(function (err, list) {
res.send(list);
});
});
app.post('/addusuario', function (req,res) {
var usuario = new Usuario(req.body);
usuario.save(function (err) {
if (err) {
console.log(err);
res.json({err: err});
} else {
res.json({ok: true});
console.log('Usuario salvo com sucesso');
}
}); });
app.delete('/delusuario/:id', function (req, res) {
var id = req.params.id;
mongoose.model('Usuario').findByIdAndRemove(id , function(err) {
if(err) {
console.log(err);
res.json({err: err});
} else {
res.json({ok: true});
console.log('Usuario removido com sucesso!');
}
});
});
You block the stack by not returning the response to the client. And this is most probably the cause of your slow request.
In my nodejs API app I have this route:
router.post('/startuserseries', function(req, res, next){
if(!req.body.username){
return res.status(400).json({message: 'Geen username'});
}
User.findOne({ 'username': req.body.username}, function(err, foundUser){
if(err)
return next(err);
if (foundUser) // check the value returned for undefined
{
foundUser.isdoingchallenges = true;
foundUser.save(function (err) {
if(err) {
console.error('ERROR!');
}
});
}
});
});
When I call this route in postman, the request never ends.
I have tried to use PUT but also didn't work, I tried various structures of code but neither worked.
This request will not finish because it doesn't write a response command on server.
You should solve easily this problem like below:
router.post('/startuserseries', function(req, res, next){
if(!req.body.username){
return res.status(400).json({message: 'Geen username'});
}
User.findOne({ 'username': req.body.username}, function(err, foundUser){
if(err)
return next(err);
if (foundUser) // check the value returned for undefined
{
foundUser.isdoingchallenges = true;
foundUser.save(function (err) {
if(err) {
res.json(err);
}
});
}
res.send(200);
// or your specific result json object
// res.json({"error":false,"message":"completed"})
});
});