javascript - How to pass data to Mongodb using Node.js, websocket and total.js -



javascript - How to pass data to Mongodb using Node.js, websocket and total.js -

i trying pass info mongodb using websocket , total.js. in homepage.html can user input , connect server via websocket after clicking save button.

in default.js server side code. @ point app hat got user input , connected server correctly, how can save info mongodb now?

this homepage.html

<br /> <div> <input type="text" name="message" placeholder="service" maxlength="200" style="width:500px" /> <button name="send" >save</div> </div> <br /> <script type="text/javascript"> var socket = null; $(document).ready(function() { connect(); $('button').bind('click', function() { if (this.name === 'send') { console.log(send()); return; } }); }); function connect() { if (socket !== null) return; socket = new websocket('ws://127.0.0.1:8000/'); socket.onopen = function() { console.log('open'); }; socket.onmessage = function(e) { var el = $('#output'); var m = json.parse(decodeuricomponent(e.data)).message; el.val(m + '\n' + el.val()); }; socket.onclose = function(e) { // e.reason ==> total.js client.close('reason message'); console.log('close'); }; } function send() { var el = $('input[name="message"]'); var msg = el.val(); if (socket !== null && msg.length > 0) socket.send(encodeuricomponent(json.stringify({ message: msg }))); el.val(''); homecoming msg; }

this default.js

exports.install = function(framework) { framework.route('/', view_homepage); framework.route('/usage/', view_usage); framework.websocket('/', socket_homepage, ['json']); }; function view_usage() { var self = this; self.plain(self.framework.usage(true)); } function view_homepage() { var self = this; self.view('homepage'); } function socket_homepage() { var controller = this; controller.on('open', function(client) { console.log('connect'); }); controller.on('message', function(client, message) { console.log(message); /* var self = this; var message = model('message').schema; var model = self.body; var message = new message({ message: model.message }).save(function(err) { if (err) self.throw500(err); // read messages message.find(self.callback()); }); */ }); controller.on('error', function(error, client) { framework.error(error, 'websocket', controller.uri); }); }

any help please!!!

this finish project

---update---

in function utilize save info mongodb didn't give error.also didnt save info database.i not sure code write or wrong

controller.on('message', function(client, message) { console.log(message); /* var self = this; var message = model('message').schema; var model = self.body; var message = new message({ message: model.message }).save(function(err) { if (err) self.throw500(err); // read messages message.find(self.callback()); }); */ });

this mongoose.js

var mongoose = require('mongoose'); mongoose.connect('mongodb://totaldemo:123456@ds029979.mongolab.com:29979/totaldemo'); global.mongoose = mongoose;

this user.js

var userschema = mongoose.schema({ user: string}) exports.schema = mongoose.model('user', userschema,'user'); exports.name = 'user';

i don't know totaljs framework @ all, see issues plain javascript.

first of all, suggest set model this:

var mongoose = require('mongoose'); var schema = mongoose.schema; var userschema = new schema({ user: string }); module.exports = mongoose.model('user', userschema);

and in controller, when import:

var user = require('path/to/user/file')

you can utilize straight away:

user.find()

also - totally dont doing later. defined user model , exported nil more string. tthing is, when import user variable user, user.name === 'user' string. in illustration be:

var user = require('path/to/user/model/file') console.log(user.name) // prints 'user'

and nil more! there no models attached export. maybe how totaljs works, uncertainty it.

later on - seek ... utilize message model. comes from? defined user model, not message model.

another thing - stated - dont know totaljs, uncertainty inquire define var model, , never utilize variable model.

i suggest using plain node mongoose first, seek integrate fullstack.

for sure not solution, maybe points out problems in code , help.

edit:

i looked in totaljs, , looks should export string (which little weird , doing magic stuff:) ), not mongoose, , guess work native totaljs model solution. cant utilize mongoose , totaljs that. dont know how much not using native totaljs models scheme ruins framework other options, safer utilize native one.

honestly, dont have time deeper docs, google says nil sql or mongo within of totaljs docs... so, have figure out :)

edit2 found https://github.com/totaljs/examples/tree/master/mongoose , looks weird... check if illustration works (looks seen it, code similar :)). check if you're mongod working, check if can conenct plain node...

honestly sorry, surrender. totaljs has much magic , abstraction me help out :(. hope find answer.

javascript node.js mongodb total.js

Comments

Popular posts from this blog

formatting - SAS SQL Datepart function returning odd values -

c++ - Apple Mach-O Linker Error(Duplicate Symbols For Architecture armv7) -

php - Yii 2: Unable to find a class into the extension 'yii2-admin' -