Last active
February 27, 2016 07:22
-
-
Save vajahath/f31f51503dcd6e60d1df to your computer and use it in GitHub Desktop.
reusable js code and reference
This file contains hidden or 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
/** | |
* @file Javascript Code Reference | |
* @author [email protected] | |
*/ | |
// create model file for mongoose model | |
// =========================================BEGIN | |
/** | |
* @file Describes the 'user' model for generating mongoose schema. | |
* @author Vajahath | |
*/ | |
var mongoose = require('mongoose'); | |
/** | |
* Modeling variable. Define the model for 'mymodel' here. | |
* @type {Object} | |
*/ | |
var mymodel = { | |
// here goes my content | |
}; | |
/** Generating mymodel schema */ | |
var mymodel_Schema = mongoose.Schema(mymodel); | |
/** Exporting module */ | |
module.exports = mongoose.model('Mymodel', mymodel_Schema); | |
// =========================================END | |
/** attaching user model */ | |
var User = require('../models/User'); | |
// save mongoose | |
object_to_save.save(function(err, saved_object) { | |
if (err) { | |
console.log("error occured in saving to db" + err); | |
} else { | |
// function after saving the object.. | |
} | |
}); | |
// mongoose findOne | |
// ================ | |
User.findOne({"criteria"}, {"optional fields to return"} function(err, person) { | |
if (err) { | |
console.error("error in findOne: " + err); | |
} else if (person === null) { | |
/** | |
* user doesnt exist in db | |
*/ | |
console.log("user doesnt exist in db"); | |
} else if (person) { | |
/** person exists in db */ | |
console.log("person already exists in db"); | |
} else { | |
console.log("something went wrong while ...: err: " + err + ", person: " + person); | |
} | |
}); | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment