[英]How to pass data to Mongodb using Node.js, websocket and total.js
I am trying to pass data to Mongodb using Websocket and total.js. 我正在尝试使用Websocket和total.js将数据传递到Mongodb。 In my
homepage.html
I can get the user input and connect to the server via websocket after clicking the save button. 单击保存按钮后,可以在我的
homepage.html
中获取用户输入并通过websocket连接到服务器。
In default.js
is my server side code. 在
default.js
是我的服务器端代码。 At this point the app hat got the user input and connected to the server correctly, but how can I save data to mongodb now? 此时,应用程序帽子已获得用户输入并正确连接到服务器,但是现在如何将数据保存到mongodb?
This is my homepage.html
这是我的
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('');
return msg;
}
This is my default.js
这是我的
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 all messages
message.find(self.callback());
});
*/
});
controller.on('error', function(error, client) {
framework.error(error, 'websocket', controller.uri);
});
}
Any help Please!!! 任何帮助请!
This is complete project 这是完整的项目
---Update--- ---更新---
In this function i use to save data to MongoDB but it didn't give any error.also Didnt save the data to database.i not sure my code is write or wrong 在此功能中,我用于将数据保存到MongoDB,但没有出现任何错误。也没有将数据保存到数据库。我不确定我的代码是写还是写错
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 all messages
message.find(self.callback());
});
*/
});
This my mongoose.js 这是我的mongoose.js
var mongoose = require('mongoose');
mongoose.connect('mongodb://totaldemo:123456@ds029979.mongolab.com:29979/totaldemo');
global.mongoose = mongoose;
This is my user.js 这是我的user.js
var userSchema = mongoose.Schema({ user: String})
exports.schema = mongoose.model('user', userSchema,'user');
exports.name = 'user';
I don't know totaljs framework at all, but i see some issues already with plain javascript. 我根本不了解totaljs框架,但是我已经发现普通javascript已经存在一些问题。
First of all, i suggest You set up Your model like this: 首先,我建议您像这样设置模型:
var mongoose = require('mongoose');
var Schema = mongoose.Schema;
var userSchema = new Schema({
user: String
});
module.exports = mongoose.model('User', userSchema);
and then in controller, when You import: 然后在控制器中,当您导入时:
var User = require('path/to/user/file')
You can use it like this straight away: 您可以像这样立即使用它:
User.find()
Also - i totally dont get what are You doing later. 另外-我完全不知道你以后在做什么。 You defined
user
model and exported NOTHING MORE than a STRING. 您定义了
user
模型,并导出了“ STRING”以外的内容。 Only tthing it will do is, that when You import that user to variable User
, the User.name
will === to 'user
' string. 唯一要做的是,当您将该用户导入变量
User
, User.name
将===转换为'user
'字符串。 so in Your example it would be: 因此在您的示例中它将是:
var User = require('path/to/user/model/file')
console.log(User.name) // prints 'user'
and nothing more! 仅此而已! There are no models attached to that export.
该出口没有附加模型。 Maybe its how totaljs works, but i VERY doubt it.
也许它是totaljs的工作方式,但我对此非常怀疑。
Later on - You try to ... use message model. 稍后-您尝试使用消息模型。 Where it comes from?
它来自哪里? You defined user model, not message model.
您定义的是用户模型,而不是消息模型。
Another thing - as i stated - i dont know totaljs, but I doubt it ask YOu to define var model
, and then never use variable model
. 另一件事-正如我所说-我不知道totaljs,但是我怀疑它要求YOu定义
var model
,然后再也不使用变量model
。
I strongly suggest using plain node with mongoose first, then try to integrate it with any fullstack. 我强烈建议首先将纯节点与猫鼬一起使用,然后尝试将其与任何全栈集成。
For sure its not a solution, but maybe it points out some problems in Your code and will help. 可以肯定的是,这不是一个解决方案,但也许它指出了您的代码中的一些问题并会有所帮助。
EDIT: 编辑:
I looked quickly in totaljs, and it looks that You really should export string (which is little weird and doing magic stuff:) ), but its NOT mongoose, and i guess will ONLY work with native totaljs model solution. 我快速浏览了totaljs,它看起来您确实应该导出字符串(这有点奇怪,并且在做魔术:)),但是它不是猫鼬,我想它只能与本机totaljs模型解决方案一起使用。 You cant use mongoose and totaljs like that.
您不能像这样使用mongoose和totaljs。 I dont know how much not using native totaljs models system ruins framework other options, but its probably safer to use native one.
我不知道有多少不使用本机totaljs模型系统会破坏框架的其他选择,但使用本机可能更安全。
Honestly, i dont have time to look deeper into docs, but google says nothing about sql or even mongo inside of totaljs docs... so, You have to figure it out :) 老实说,我没有时间更深入地研究文档,但是Google在TotalJS文档中没有提到SQL甚至mongo ...所以,您必须弄清楚:)
EDIT2 i found https://github.com/totaljs/examples/tree/master/mongoose and it looks weird... check if that example works (looks like You seen it, Your code is similar :)). EDIT2我发现https://github.com/totaljs/examples/tree/master/mongoose ,它看起来很奇怪...检查该示例是否有效(看起来像您看到的那样,您的代码很相似:))。 check if You're mongod is working, check if You can conenct from plain node...
检查您是否在使用mongod,检查是否可以从纯节点连接...
Honestly sorry, i surrender. 老实说对不起,我投降了。 Totaljs has to much magic and abstraction for me to help You out with this :(. Hope You will find Your answer.
Totaljs对我来说具有极大的魔力和抽象性,可以帮助您解决这个:(。希望您能找到答案。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.