Skip to content

Instantly share code, notes, and snippets.

@vajahath
Last active February 27, 2016 07:22
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save vajahath/f31f51503dcd6e60d1df to your computer and use it in GitHub Desktop.
Save vajahath/f31f51503dcd6e60d1df to your computer and use it in GitHub Desktop.
reusable js code and reference
/**
* @file Javascript Code Reference
* @author vajuoff.1@gmail.com
*/
// 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