Hello
I permitted myself to publish this post because I can't find a way.
I followed the Chatroom tutorial in this site: http://docs.appcelerator.com/cloud/latest/#!/guide/node_quickstart. Everything worked fine.
Next, I tried the connection to MongoDB running the example in Local Node.ACS Server mode by putting the following code in the chatroom.js.
//chatroom.js function receiveMessage(data, socket) {
console.log("mensaje recibido: "+data.message);
console.log("connectando DB 1"); // Connect to the db var MongoClient = require('mongodb').MongoClient;
//console.log(MongoClient);
MongoClient.connect("mongodb://localhost:27017/dbTest", {native_parser:true},
function(err, db) {
if(!err)
{
console.log("Abriendo Collection");
db.collection('messages', function(err, collection) {
console.log("insertando mensaje");
collection.insert(data, {w:1}, function(err, result) {
console.log(result);
});
});
}
else
console.log("Error en la conexion: " + err);
});
console.log("enviando mensaje a todos");
socket.broadcast.emit('message', data); }
The issue is that the callback is not being executed. I tested the same connection code separately on Node by running: node testMongo.js and works perfectly:
//testMongo.js
var data={message:'greetings!!'};
// Connect to the db
var MongoClient = require('mongodb').MongoClient;
//console.log(MongoClient);
MongoClient.connect("mongodb://localhost:27017/dbTest", {native_parser:true},
function(err, db) {
if(!err)
{
console.log("Abriendo Collection");
db.collection('messages', function(err, collection) {
console.log("insertando mensaje");
collection.insert(data, {w:1}, function(err, result) {
console.log(result);
});
});
}
else
console.log("Error en la conexion: " + err);
});
MongoClient.connect("mongodb://localhost:27017/dbTest", function(err, db) { if(err) {
return console.dir(err);
}
var collection = db.createCollection('messages', function(err, collection) {
collection.find().toArray(function(err, items) {
console.log(items);
});
});
});
For reasons I don't understand, the MongoClient.connect() function on Chatroom.js gets executed but the callback doesn't. Not even to show any error. The only way I can get the callback to run is by causing a wrong port connection error. Otherwise, it shows nothing on Titanium's console. Is like the connection never happens.
Does anybody know what is happening?
Thanks a lot!
PS:
Titanium Studio, build: 3.1.1.201306112235 Build: jenkins-titanium-rcp-master-466 (origin/master) Date: 11 June 2013, 22:38:40
Node v0.10.13 MongoD db version v2.4.5 ACS v1.0.6