daily pastebin goal
4%
SHARE
TWEET

Untitled

a guest Mar 20th, 2017 69 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. Users = mongoose.UserSchema;
  2. ApiUserSchema;    
  3. Users.find(function(err,users){
  4.     //for each user that mongoose finds,
  5.     //I want to convert from UserSchema to "ApiUserSchema"
  6.     //Something like the following:
  7.     newUsers = users.ToApiUserSchema();
  8.     console.log(newUsers);
  9. })
  10.    
  11. function toApiUserSchema(user){
  12.      username = user.username;
  13.      email = user.email;
  14.      someAggregate = user.Username + user.Email;
  15.      .......and so on.....;
  16. }
  17.    
  18. var userSchema = new mongoose.Schema({
  19.     username: String,
  20.     email: String,
  21.     password: Number
  22. })
  23.  
  24. userSchema.statics.toApiUserSchema = function(data) {
  25.     return data.map(function(user) {
  26.         return {
  27.             email: user.email,
  28.             username: user.username,
  29.             emailAndUsername: user.email + user.username
  30.         }
  31.     })
  32. }
  33.  
  34. var User = mongoose.model('User', userSchema)
  35.  
  36. User.find({}, function(err, users) {
  37.     console.log(User.toApiUserSchema(users))
  38. })
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top