Created
February 8, 2012 21:28
fixing issue: http://stackoverflow.com/questions/9184863/mongoose-push-doesnt-work-inside-socket-io-call/9194446#comment11580668_9194446
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//Storify -- Khuram Malik | |
// Required Modules | |
var app = require('express').createServer() | |
, io = require('socket.io').listen(app) | |
, express = require('express') | |
, mongoose = require ('mongoose') | |
, Schema = mongoose.Schema; | |
app.listen(8090); | |
app.get('/', function (req, res) { | |
res.sendfile((__dirname + '/story.html')); | |
}); | |
app.use(express.static(__dirname + '/public')); | |
//Create Schema | |
var Lines = new Schema({ | |
author : String, | |
text : String | |
}); | |
var Story = new Schema ({ | |
maxlines: {type: Number, default: 3}, // Max number of lines per user | |
date: {type: Date, default: Date.now}, | |
title: String, | |
lines: [Lines] | |
}); | |
mongoose.connect('mongodb://localhost/test'); | |
//setup model and pass it schema | |
mongoose.model ('Story',Story); | |
var StoryModel = mongoose.model ('Story'); | |
var story = new StoryModel(); | |
// Fixed params | |
story.title = 'Last Samurai'; | |
//Capture data from socket into schema | |
io.sockets.on('connection', function (socket) { | |
socket.emit('news', { hello: 'world' }); | |
socket.on('slog', function (data) { | |
console.log("SAVING"); | |
story.lines.push ({ author: 'Khuram', text: data.my}) | |
story.save(function(err){ | |
//save line | |
if (err) {throw err; } | |
console.log('saved story line'); | |
}); | |
}); | |
}); | |
//disconnect db | |
//mongoose.disconnect(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment