Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save mikermcneil/483987369d54512b6104 to your computer and use it in GitHub Desktop.
Save mikermcneil/483987369d54512b6104 to your computer and use it in GitHub Desktop.
This works in Sails v0.10.0-rc7 (Waterline v0.10.0-rc12)
/**
* PetController
*
* @description :: Server-side logic for managing pets
* @help :: See http://links.sailsjs.org/docs/controllers
*/
module.exports = {
index: function (req,res) {
Pet.native(function(err, collection) {
if (err) return res.serverError(err);
collection.find({}, {
name: true
}).toArray(function (err, results) {
if (err) return res.serverError(err);
console.log('->',results);
return res.ok(results);
});
});
}
};
@avalla
Copy link

avalla commented Nov 25, 2016

@Rafi993 I used this syntax:

return new Promise((resolve, reject) => {
  Pet.native((err, collection) => {
    if (err) reject(err)
    else collection.find({}, {
      name: true
    }).toArray((err, results) => {
      if (err) reject(err)
      else resolve(results)
    });
  })
}

@dilame
Copy link

dilame commented Aug 20, 2017

@Rafi993
const collection = await Promise.fromCallback(Model.native);

@sksbtps
Copy link

sksbtps commented Aug 23, 2017

what are the methods that collection supports? because when I do collection.stats It works but it throws an error for collection.serverStats

@MisterFredy
Copy link

what's the meaning of collection.find({}, {
name: true
})?? collection is name of our collection? or Pet.native is for name of collection?

@cope
Copy link

cope commented Oct 21, 2020

what's the meaning of collection.find({}, {
name: true
})?? collection is name of our collection? or Pet.native is for name of collection?

Neither. Collection is the collection instance.

If your collection is called pet, then your model is most likely Pet. Then: Pet.native((err, petCollection) => { would probably be more clear?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment