You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

49 lines
1.9 KiB

7 years ago
  1. //File: controllers/userController.js
  2. var mongoose = require('mongoose');
  3. var userModel = mongoose.model('userModel');
  4. var notificationModel = mongoose.model('notificationModel');
  5. var travelModel = mongoose.model('travelModel');
  6. /* */
  7. var jwt = require('jsonwebtoken'); // used to create, sign, and verify tokens
  8. var express = require("express");
  9. var app = express();
  10. var config = require('../config'); // get our config file
  11. app.set('superSecret', config.secret); // secret variable
  12. var crypto = require('crypto');
  13. /* */
  14. exports.searchByString = function (req, res) {
  15. console.log(req.params.searchstring);
  16. userModel.find({
  17. username: new RegExp(req.params.searchstring, "i")
  18. })//perquè retorni tots els objectes que continguin l'string sense necessitat de que sigui exactament la mateixa string
  19. .limit(Number(req.query.pageSize))
  20. .skip(Number(req.query.pageSize) * Number(req.query.page))
  21. .lean()
  22. .select('username avatar')
  23. .exec(function (err, users) {
  24. if (err) return res.send(500, err.message);
  25. travelModel.find({
  26. $or:[
  27. {from: new RegExp(req.params.searchstring, "i")},
  28. {to: new RegExp(req.params.searchstring, "i")},
  29. {title: new RegExp(req.params.searchstring, "i")}
  30. ]
  31. })//perquè retorni tots els objectes que continguin l'string sense necessitat de que sigui exactament la mateixa string
  32. .limit(Number(req.query.pageSize))
  33. .skip(Number(req.query.pageSize) * Number(req.query.page))
  34. .lean()
  35. .select('title from to date type')
  36. .exec(function (err, travels) {
  37. if (err) return res.send(500, err.message);
  38. res.json({
  39. users: users,
  40. travels: travels
  41. });
  42. });//travels
  43. });//users
  44. };