Skip to content

Instantly share code, notes, and snippets.

@Ripley6811
Last active August 29, 2015 14:14
Show Gist options
  • Save Ripley6811/da4be0a186e6ced1f22b to your computer and use it in GitHub Desktop.
Save Ripley6811/da4be0a186e6ced1f22b to your computer and use it in GitHub Desktop.

This is a personalized reference for functions I have used. Any excluded parameters or simplifications are intentional.


##Function Summary

function param1 param2 response
.find PK or {} NA [{}]
.findOrCreate PK or {} {} {}
.destroy PK or {} NA [{}]
.update PK or {} {} [{}]
PK = primary key; int or string.
{} = Single record.
[{}] = An array of records.

##Errors ###Uncaught SyntaxError: Unexpected end of input Problem: Callbacks continue to execute after a res.send(). I keep assuming the block will end like at a return;, but the callback continues and encountering another send() will cause this type of error.

Fix: Either add return; after every send() call or use multiple if-else-if bracketing to prevent executing code after a res.send().

###criteria not found Problem: Getting a criteria not found error when using a string or int to find a record. Probably occurs if a string id is not getting properly cast as an int or vice versa when searching the database.

Fix: Use bracketed (JSON) notation for the criteria. Model.find({id: pkvalue}, cb) and not Model.find(pkvalue, cb).

##Warning ###Using create with an array or new records will not return an array in the same order.

##Find Model.find(criteria, function (err, record) {}); Model.find(criteria).exec(function (err, record) {});

  • criteria = primary key value or json matching
  • function = Callback
  • reclist = Array of record objects
    • NOTE: Always returns an array.

##Find or Create Model.findOrCreate(criteria, object, function (err, record) {}); Model.findOrCreate(criteria, object).exec(function (err, record) {});

  • criteria = primary key value or json matching
    • NOTE: criteria not found error may require brackets around named id or criteria {id: value}.
  • object = record to create.
    • NOTE: Can contain additional parameters which are ignored.
  • function = Callback
  • record = record object
    • NOTE: I can only get this to work on single records so far.

##Destroy Model.destroy(criteria, function (err, recList) {}); Model.destroy(criteria).exec(function (err, recList) {});

  • criteria = Find Criteria {},[{}], string, int
  • function = Callback
  • recList = Always returns an Array

##Update Model.update(criteria, object, function (err, recList) {}); Model.update(criteria, object).exec(function (err, recList) {});

  • criteria = Find Criteria {},[{}], string, int
  • object = record to create.
    • NOTE: Can contain additional parameters which are ignored.
  • function = Callback
  • recList = Always returns an Array
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment