findOneAndUpdate fails using MEAN stack - javascript

Here's my code on my layout.handlebars
$(document).ready(function(){
var socket = io();
// //understand button
$(".understandbtn").click(function(){
//reset the timer every 3 second of interval
$('.actionBtnFloat').css('z-index','0');
//e_money
var deduct = 100;
var newMoney = {{user.e_money}} - deduct;
// send a message to the server that the e-money value has changed
//get the current user
socket.emit('update e-money',getUserName(), newMoney);
// console.log("Emitting the data to the server side - emoney" + getUserName() + "with the name money of :" + newMoney);
//end
clearTimeout(interval);
//send the data to the server
socket.emit('chat message', getUser());
var interval = setTimeout(function(){
$('.'+getUser()).fadeIn();
},5000);
});
socket.on('update e-money response', function (data) {
alert("Your money is: "+ data.newMoney);
console.log("Your money is:" + data.newMoney);
});
socket.on('update e-money error', function (err,data) {
if(err) throw err;
// alert("Could not update your money: "+ data.error);
// console.log("Could not update your money"+ data.error);
alert("Sucessfully updated your money");
console.log("Sucessfully updated your money");
});
And on my server here it is how i update my record but its not working
is it because of the error?
//emoney
socket.on('update e-money', function (data) {
var userName = data.username;
var newMoney = data.newMoney;
var query = {username: userName};
// update the entry on the database
User.findOneAndUpdate(query, { e_money: newMoney }, { upsert: true, new: true }, function (err, doc) {
if (err) {
io.emit('update e-money error', { error: err });
console.log(err);
} else {
io.emit('update e-money response', { e_money: newMoney });
console.log(newMoney);
}
});
});
//end emoney
Now it says couldn't update my record
Is it because im not using the _id instead?
here's my error
message: 'Cast to number failed for value "undefined" at path "e_money"',
name: 'CastError',
stringValue: '"undefined"',
kind: 'number',
value: undefined,
path: 'e_money',
reason: undefined }

By doing this achieved what i want
User.findOneAndUpdate({"username":userName},
{"$set":{"e_money": newMoney }}, { upsert: true, returnOriginal:false },
function (err, doc) {

Related

nodejs mongoDB findOneAndUpdate(); returns true even after database is updated

i am working on an Ionic-1 + nodejs + angular application. My mongoDb findOneAndUpdate() function returns true on each call even the first call updates database.
nodejs:
app.post('/booking', function (req, res) {
var collection = req.db.get('restaurant');
var id = req.body.id;
var status = req.body.status;
collection.findOneAndUpdate({status: status, id: id},{$set:{status:"booked"}}, function (e, doc) {
console.log(id, status);
if (e) {
console.log(e);
}
else if(!doc) {
res.send(false);
}
else {
res.send(true);
}
});
});
controller.js
$scope.bookMe = function(id){
var Obj = {status: "yes", id: id};
myService.booking(Obj).success(function(res){
console.log(Obj, "Checking status")
console.log(res);
if (res == true) {
var alertPopup = $ionicPopup.alert({
title: 'Booking Confirm',
template: 'Thanks For Booking'
});
}
else{
var alertPopup = $ionicPopup.alert({
title: 'Error',
template: ' Not available'
});
}
})
};
where i am doing wrong. my DB gets updated but it returns true always on next call.
The documentation about findOneAndUpdate says :
Finds a matching document, updates it according to the update arg, passing any options, and returns the found document (if any) to the callback. The query executes immediately if callback is passed.
So it's regular behavior you got a doc.
Note:
Since you are checking availability status="yes", Better hard code, instead of getting it from request query/data.
Change the response according to your requirement res.send(true)/ res.send(false).
Following code will work
app.post('/booking', function (req, res) {
var collection = req.db.get('restaurant');
collection.findOneAndUpdate({
status: "yes",
_id: req.body.id
}, {
$set: {
status: "booked"
}
}, function (err, result) {
//Error handling
if (err) {
return res.status(500).send('Something broke!');
}
//Send response based on the required
if (result.hasOwnProperty("value") &&
result.value !== null) {
res.send(true);
} else {
res.send(false);
}
});
});

Async - Can't set headers after they are sent

I am new to node and async...
I am getting an error saying I can't set headers after they sent when I am sending a response back to api-ai
Any idea why?
Below is the code for function - getUserFirstName(userId, name, callback):
var name = "";
function getUserFirstName(userId, name, callback) {
console.log('withParams function called');
request({
method: 'GET',
uri: "https://graph.facebook.com/v2.6/" + userId + "?fields=first_name,last_name,profile_pic,locale,timezone,gender&access_token=" + FB_PAGE_ACCESS_TOKEN
},
function (error, response) {
if (error) {
console.error('Error while userInfoRequest: ', error);
} else {
if(!typeof response.body != 'object'){
var body = JSON.parse(response.body);
name = body.first_name;
callback(null,name);
}else{
name = response.body.first_name;
callback(null,name);
}
}
});
}
Here is the code being executed:
app.post('/webhook/', (req, res) => {
var data = JSONbig.parse(req.body);
var action = data.result.action;
var facebook_message = [];
if(action == "input.welcome"){
var userId = data.originalRequest.data.sender.id;
async.series([
function(callback) {
getUserFirstName(userId, name, callback);
}
], function(err,results) {
if (results != undefined){ // results = "John"
facebook_message = [{
"text":"Heyyyoo. Welcome!"
}]
}else{
facebook_message = [{
"text":"Hey " + results +"! Welcome!" // Hey John! Welcome!
}]
}
res.json({ // line 308 - error here!
speech: "Greetings",
displayText: "Greetings",
"data": {
"facebook": facebook_message
},
source: "webhook"
});
});
}
// BUNCH OF LONG AND MESSY CODES OVER HERE...
return res.status(200).json({
status: "ok"
});
Error
Error: Cant set headers after they are sent.
at ServerResponse.OutgoingMessage.setHeader (_http_outgoing.js:356:11)
at ServerResponse.header (/app/node_modules/express/lib/response.js:719:10)
at ServerResponse.send (/app/mode_modules/express/lib/response.js:164:12)
at ServerRespose.json (/app/mode_modules/express/lib/response.js:250:15)
at /app/src/app.js: 308:15
at /app/node_modules/async/dist/async.js:3694:9
at /app/node_modules/async/dist/async.js:356:16
at replenish (/app/node_modules/async/dist/async.js:877.25)
at iterateeCallback (/app/node_modules/async/dist/async.js:867:17)
at /app/node_modules/async/dist/async.js:840:16
Remove the following:
return res.status(200).json({
status: "ok"
});

query results are undefined

exports.save = function(req, res) {
connection.query('INSERT INTO student_details(name, course, units, grades, gwa) VALUES(?, ?, ?, ?, ?)',[req.body.name,req.body.course,req.body.units,req.body.grades,req.body.gwa], function(err, row) {
if(err) res.send('Error in query');
selectOne(row.insertId, function(newRow){
if(err) res.status(554).send('Error in query');
if(newRow == null){
res.send(552, {message: 'Student Details ('+row.insertId+') was not created.'});
} else{
res.status(200).send(newRow);
}
});
});
}
var selectOne = function(id, callback){
connection.query('SELECT * FROM student_details WHERE id=? LIMIT 1', [id], function(err, rows){
if(err) return err;
if(rows.length != 0){
callback(rows[0]);
}else{
callback(null);
}
});
}
I'm having an error performing the query above. It's saying this error:
Uncaught TypeError: Cannot read property 'insertId' of undefined.
I've been trying to figure this out for hours now, and my hunch is that it's somewhat related to asynchronous values but I can't figure out how to fix it.
UPDATE:
This is my test file:
var studentdb = require(__dirname + '/../studentdb'),
student = require(__dirname + '/../student'),
index = require(__dirname + '/../index'),
should = require('should-http'),
assert = require('assert'),
request = require('supertest');
describe('Student Details', function() {
var url = 'http://localhost:1337';
var randomName = student.getRandomName(10);
var insertedId = 0;
describe('insert()', function () {
it('should create a new student record', function (done) {
var input = {
nameLength: 10,
subjectsLength: 5,
course: 'CAS'
};
studentName = student.getRandomName(input.nameLength);
studentCourse = student.getRandomCourse(input.course);
studentUnits = student.getRandomUnits(input.subjectsLength);
studentGrades = student.getRandomGrades(input.subjectsLength);
studentGWA = student.computeGWA(studentUnits, studentGrades,input.subjectsLength);
var stringUnits = studentUnits.join();
var stringGrades = studentGrades.join();
var generatedStudent = {
'name': studentName,
'course': studentCourse,
'units': stringUnits,
'grades': stringGrades,
'gwa': studentGWA
}
request(url)
.post('/addStudent')
.send(generatedStudent)
.end(function(err, res) {
if (err) {
throw err;
}
res.should.have.status(200);
res.body.should.have.keys(['id', 'name', 'course','units', 'grades', 'gwa']);
done();
});
});
});
describe('save()', function() {
console.log(insertedId);
it('should generate a student record', function(done) {
request(url)
.get('/generateStudent')
.end(function(err, res) {
if(err) throw err;
res.should.have.status(200);
res.body.should.not.have.property('name', null);
res.body.should.not.have.property('course', null);
generatedStudent = res.body;
done();
});
});
it('should not have duplicate name', function(done) {
request(url)
.get('/getStudents')
.end(function(err, res) {
if(err) throw err;
res.body.forEach(function(iteration) {
assert.notEqual(iteration, generatedStudent.name);
});
done();
});
});
it("now adding to the database", function(done) {
request(url)
.post('/addStudent')
.send(generatedStudent)
.end(function(err, res) {
if(err) throw err;
res.body.should.have.status(200);
console.log(res.body.id);
res.body.should.have.keys(['id', 'name', 'course', 'units', 'grades', 'gwa']);
done();
});
});
});
});
----------SECOND UPDATE after following Zeeshan's code------------------
they keep on rejecting my edit in the comment section.
Again, I've tried your suggestion but still no cigar. I've also printed the "newRow" to show that the data is being retrieved properly, it's just that when I try the res.send function, it is being read as undefined therefore causing the error. Thank you for your patience!
Server Connected on port: 1337
Student Details
insert()
Database is connected ... nn
If error, I should not be printed
Value of row.insertId 34
{ id: 34,
name: 'H3tno72Osk',
course: 'BS Computer Science',
units: '4,1,2,4,2',
grades: '3.0,3.0,3.0,3.0,4.0',
gwa: 3 }
✓ should create a new student record (66ms)
save()
✓ should generate a student record
✓ should not have duplicate name
1) now adding to the database
3 passing (90ms)
1 failing
1) Student Details save() now adding to the database:
Uncaught AssertionError: expected Object { body: undefined, statusCode: undefined } to have property statusCode of 200 (got undefined)
at Assertion.fail (node_modules/should/lib/assertion.js:92:17)
at Assertion.Object.defineProperty.value [as status] (node_modules/should/lib/assertion.js:164:19)
at Test.<anonymous> (test/studentdb.js:86:27)
at Test.assert (node_modules/supertest/lib/test.js:156:6)
at assert (node_modules/supertest/lib/test.js:127:12)
at node_modules/supertest/lib/test.js:124:5
at Test.Request.callback (node_modules/supertest/node_modules/superagent/lib/node/index.js:691:12)
at IncomingMessage.<anonymous> (node_modules/supertest/node_modules/superagent/lib/node/index.js:922:12)
at _stream_readable.js:920:16
Modified a bit, Can you try this as well and update with results?
exports.save = function(req, res) {
connection.query('INSERT INTO student_details(name, course, units, grades, gwa) VALUES(?, ?, ?, ?, ?)',[req.body.name,req.body.course,req.body.units,req.body.grades,req.body.gwa], function(err, row) {
if(err) return res.send('Error in query');
console.log("If Errror I should not pe printed");
console.log("Value of row.insertId ", row.insertId);
selectOne(row.insertId, function(newRow, insertId){
if(err) res.status(554).send('Error in query');
if(newRow == null){
res.send(552, {message: 'Student Details ('+insertId+') was not created.'});
} else{
res.status(200).send(newRow);
}
});
});
}
var selectOne = function(id, callback){
connection.query('SELECT * FROM student_details WHERE id=? LIMIT 1', [id], function(err, rows){
if(err) return err;
if(rows.length != 0){
callback(rows[0], id);
}else{
callback(null, id);
}
});
}
So, I have managed to get the result that I wanted. I ended up comparing the id count from the insert() function, instead of the save() function.
describe('insert()', function () {
it('should create a new student record', function (done) {
var input = {
nameLength: 10,
subjectsLength: 5,
course: 'CAS'
};
studentName = student.getRandomName(input.nameLength);
studentCourse = student.getRandomCourse(input.course);
studentUnits = student.getRandomUnits(input.subjectsLength);
studentGrades = student.getRandomGrades(input.subjectsLength);
studentGWA = student.computeGWA(studentUnits, studentGrades, input.subjectsLength);
var stringUnits = studentUnits.join();
var stringGrades = studentGrades.join();
var generatedStudent = {
'name': studentName,
'course': studentCourse,
'units': stringUnits,
'grades': stringGrades,
'gwa': studentGWA
}
request(url)
.post('/addStudent')
.send(generatedStudent)
.end(function(err, res) {
if (err) {
throw err;
}
res.should.have.status(200);
insertedId = res.body.id;
res.body.should.have.keys(['id', 'name', 'course', 'units', 'grades', 'gwa']);
done();
});
});
});
You should exec return if got err. The simplest way to do this is if(err) return res.send('Error in query');.

angular promises and nodejs http get response

I would use the promises of angularJS to fill data to a grid. I'd like to load data "row by row" as soon as the nodeJS's server, on which use the module "mssql" with the "stream" enabled, back to client every single line from the DB.
On the client side I use these functions:
function asyncGreet() {
var deferred = $q.defer();
var _url = 'http://localhost:1212/test';
$http.get(_url).
then(function(result) {
deferred.resolve(result);
}, function(error) {
deferred.reject(error);
}, function(value) {
deferred.notify(value); //<<-- In "value" I would like to get every single row
});
return deferred.promise;
}
$scope.btnTest = function () {
var promise = asyncGreet();
promise.then(function(res) {
console.log('Success: ' + res.data + "\n");
}, function(reason) {
console.log('Failed: ' + reason);
}, function(update) {
console.log('Got notification: ' + update); //<<--
});
};
On nodeJS server those:
app.get('/test', function (req, res) {
//sql for test
var _query = 'select top 50 * from tb_test';
var sql = require('mssql');
var connection;
var config = {
user: 'testUser',
password: '12345',
server: 'localhost\\test',
database: 'testDB',
stream: true
};
connection = new sql.Connection(config, function (err) {
var request = new sql.Request(connection);
request.query(_query);
request.on('recordset', function(columns) {
// Emitted once for each recordset in a query
//res.send(columns);
});
request.on('row', function(row) {
res.write(JSON.stringify(row)); //<<-- I would like intercept this event on client side
// and get the result in my angularJS function on deferred.notify
});
request.on('error', function(err) {
// May be emitted multiple times
console.error(err)
});
request.on('done', function(returnValue) {
// Always emitted as the last one
res.end('DONE');
});
});
});
Anyone can help me with this?
Thanks!
I'm done it using socket.io :)
On angularJS side:
// count the row for test only
$scope.count = 0;
$scope.prova = function () {
mySocket.emit('getTableByRow', {});
mySocket.on('resRow', function (data) {
if (data.event == 'ROW') {
$scope.count += 1;
}else {
$scope.count += " !!DONE!! ";
}
});
};
On NodeJS side:
[ ... connection with DB ... ]
io.on('connection', function (socket) {
socket.on('getTableByRow', function (data) {
_getTableByRow(socket, data);
});
});
_getTableByRow function:
var _getTableByRow = function (socket, data) {
var _query = 'select top 50 * from tb_test';
request.query(_query);
request.on('row', function(row) {
// return only the ids for test
socket.emit('resRow', {event: 'ROW', data: row.id.toString()});
});
request.on('done', function(returnValue) {
socket.emit('resRow', {event: 'DONE'});
});
request.on('recordset', function(columns) {
console.log(columns);
});
request.on('error', function(err) {
socket.emit('resRow', {event: 'ERROR', data: err});
});
}
In this way, as soon as one row is read from the DB, it is immediately sent to the client :)

Parse updating a set of objects

I have trouble updating a set of values in my cloud code. I have tried .save() seperately and .saveAll() but the class doesn't get updated in Parse and I get errors returned.
What I am trying to do is to get all messages from class ChatMessages which has a pointer to the Parse user and Chat class. When the method is called, the class column readAt needs to be updated to the current date. I call my method from an iOS (objective-C) app.
This is the latest version of my method:
Parse.Cloud.define("markChatAsReadForRoomAndUser", function(request, response) {
var errorMsg;
var roomName;
var _ = require('underscore.js');
var userPointer = new Parse.User.current();
if (!request.params.roomName) {
errorMsg = "Chat room needs to be identified";
} else {
roomName = request.params.roomName;
}
console.log("Checking chats for userID: " + userPointer.id);
if (!userPointer.id) {
var emptyUserMsg = "User has to be provided";
if (errorMsg) {
errorMsg = errorMsg + emptyUserMsg;
} else {
errorMsg = emptyUserMsg;
};
}
if (errorMsg) {
response.error(errorMsg);
}
var chatQuery = new Parse.Query("Chat");
chatQuery.equalTo("roomName", roomName);
chatQuery.find({
success: function(results) {
if (results.length > 0) {
var chat = results[0];
console.log("Found chat with ID: "+chat.id);
var chatMessagesQuery = new Parse.Query("ChatMessage");
chatMessagesQuery.equalTo("chat", chat);
chatMessagesQuery.notEqualTo("fromUser", userPointer);
chatMessagesQuery.equalTo("readAt", undefined);
chatMessagesQuery.find().then(function(chatMessagesQueryResults) {
_.each(chatMessagesQueryResults, function(result) {
result.set("readAt", new Date());
console.log("Setting readAt for chat message " + result.id + " which has toUser " + result.get("toUser"));
});
return Parse.Object.saveAll(chatMessagesQueryResults,{
success: function(list) {
console.log("Success updating objects");
},
error: function(error) {
console.log("Error updating objects: " + error);
},});
}).then(function(results) {
response.success(results);
console.log("Update for reatAt for chat is successfull");
}, function(error) {
response.error(error);
console.log(error);
});
} else {
response.error("No rooms found");
console.log("No rooms found");
}
},
error: function(error) {
response.error("Room name not found");
console.log(error);
}
});
});
Log output:
E2015-07-19T09:13:48.483Z]v337 Ran cloud function markChatAsReadForRoomAndUser for user CZwQL4y751 with:
Input: {"roomName":"room_czwql4y751_uoc7rjxwpo"}
Result: {"code":101,"message":"object not found for update"}
I2015-07-19T09:13:48.540Z]Checking chats for userID: CZwQL4y751
I2015-07-19T09:13:48.593Z]Found chat with ID: gfvAkirqTs
I2015-07-19T09:13:48.647Z]Setting readAt for chat message ZiWUIdUtUm which has toUser undefined
I2015-07-19T09:13:48.648Z]Setting readAt for chat message YHEBLpR04U which has toUser undefined
I2015-07-19T09:13:48.649Z]Setting readAt for chat message 0wZ4LQd8ZC which has toUser undefined
I2015-07-19T09:13:48.650Z]Setting readAt for chat message MYsYGyXI0k which has toUser undefined
I2015-07-19T09:13:48.751Z]Error updating objects: [object Object]
I2015-07-19T09:13:48.752Z]{"code":101,"message":"object not found for update"}
E2015-07-19T09:13:49.042Z]v337 Ran cloud function markChatAsReadForRoomAndUser for user CZwQL4y751 with:
Input: {"roomName":"room_czwql4y751_uoc7rjxwpo"}
Result: {"code":101,"message":"object not found for update"}
Class:
The query can be vastly simplified by making a chatMessages query relational to chats matching the user and room criteria. The code structure can be improved by not mixing callback and promise styles, and by separating logical chunks into small, promise-returning functions.
Stripping away some of the debug instrumentation you added, we get (untested, of course)...
function unreadChatMessagesInRoom(roomName, excludeUser) {
var query = new Parse.Query("ChatMessage");
query.notEqualTo("fromUser", excludeUser);
query.doesNotExist("readAt");
var chatQuery = new Parse.Query("Chat");
chatQuery.equalTo("roomName", roomName);
query.matchesQuery("chat", chatQuery);
return query.find();
}
Parse.Cloud.define("markChatAsReadForRoomAndUser", function(request, response) {
var _ = require('underscore.js');
var user = request.user;
unreadChatMessagesInRoom(request.params.roomName, user).then(function(chatMessages) {
console.log(chatMessages.length + " chat messages found");
_.each(chatMessages, function(chatMessage) {
chatMessage.set("readAt", new Date());
});
return Parse.Object.saveAll(chatMessages);
}).then(function(results) {
response.success(results);
}, function(error) {
response.error(error);
});
});

Categories

Resources