Guest User

Untitled

a guest
Jan 9th, 2018
148
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.45 KB | None | 0 0
  1. var roomModel = require('../../../models/room').roomModel;
  2.  
  3. roomModel.findOne({ name: req.body.roomName })
  4. .then(
  5. (room) => {
  6. return new Promise(function(resolve, reject) {
  7. //if no room present, create one, if present, check password
  8. if (room) {
  9.  
  10. if (room.password === req.body.roomPassword) {
  11. return resolve(room);
  12. } else {
  13. return reject({
  14. code: 401,
  15. message: 'Room password not correct'
  16. });
  17. }
  18. } else {
  19. // create new room with given data
  20. var newRoom = roomModel({});
  21. newRoom.name = req.body.roomName;
  22. newRoom.password = req.body.roomPassword;
  23. //newRoom.users = [];
  24. newRoom.users[0] = {
  25. name: req.body.userName
  26. };
  27.  
  28. newRoom.save()
  29. .then((data) => {
  30. console.log(data);
  31. if (!data) {
  32. return reject({
  33. code: 500,
  34. message: 'Error when saving room'
  35. });
  36. } else {
  37. return resolve(newRoom);
  38. }
  39. });
  40. }
  41. });
  42. }
  43. )
  44. .then((room) => {
  45. room.findOne({ 'users.name': req.body.userName })
  46. .then((user) => {
  47. console.log(user);
  48. });
  49. })
  50.  
  51. 'use strict';
  52. var mongoose = require('mongoose');
  53. var Schema = mongoose.Schema;
  54. var userSchema = require('./user').userSchema;
  55.  
  56. var room = new Schema({
  57. name: String,
  58. password: String,
  59. users: [userSchema]
  60. });
  61.  
  62. module.exports.roomSchema = room;
  63. module.exports.roomModel = mongoose.model('room', room);
  64.  
  65. var mongoose = require('mongoose');
  66. var Schema = mongoose.Schema;
  67.  
  68. var user = new Schema({
  69. name: String
  70. });
  71.  
  72. module.exports.userSchema = user;
  73. module.exports.userModel = mongoose.model('user', user);
  74.  
  75. TypeError: room.findOne is not a function
  76.  
  77. roomModel.findOne({ name: req.body.roomName }).exec().then(/* your stuff */)
  78.  
  79. roomModel.find({ name: req.body.roomName }).exec().then(/* your stuff */)
Add Comment
Please, Sign In to add comment