Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- var roomModel = require('../../../models/room').roomModel;
- roomModel.findOne({ name: req.body.roomName })
- .then(
- (room) => {
- return new Promise(function(resolve, reject) {
- //if no room present, create one, if present, check password
- if (room) {
- if (room.password === req.body.roomPassword) {
- return resolve(room);
- } else {
- return reject({
- code: 401,
- message: 'Room password not correct'
- });
- }
- } else {
- // create new room with given data
- var newRoom = roomModel({});
- newRoom.name = req.body.roomName;
- newRoom.password = req.body.roomPassword;
- //newRoom.users = [];
- newRoom.users[0] = {
- name: req.body.userName
- };
- newRoom.save()
- .then((data) => {
- console.log(data);
- if (!data) {
- return reject({
- code: 500,
- message: 'Error when saving room'
- });
- } else {
- return resolve(newRoom);
- }
- });
- }
- });
- }
- )
- .then((room) => {
- room.findOne({ 'users.name': req.body.userName })
- .then((user) => {
- console.log(user);
- });
- })
- 'use strict';
- var mongoose = require('mongoose');
- var Schema = mongoose.Schema;
- var userSchema = require('./user').userSchema;
- var room = new Schema({
- name: String,
- password: String,
- users: [userSchema]
- });
- module.exports.roomSchema = room;
- module.exports.roomModel = mongoose.model('room', room);
- var mongoose = require('mongoose');
- var Schema = mongoose.Schema;
- var user = new Schema({
- name: String
- });
- module.exports.userSchema = user;
- module.exports.userModel = mongoose.model('user', user);
- TypeError: room.findOne is not a function
- roomModel.findOne({ name: req.body.roomName }).exec().then(/* your stuff */)
- roomModel.find({ name: req.body.roomName }).exec().then(/* your stuff */)
Add Comment
Please, Sign In to add comment