These are chat archives for Automattic/mongoose

1st
Sep 2018
imjeen
@imjeen
Sep 01 2018 02:57
How do you make the error messages international in a good way?
matrixbot
@matrixbot
Sep 01 2018 09:38
ZehN Hi. Wondering if anyone could give me a help with mongoose. To give an idea of what I have here. I have two Schemas: Device and Message and one Device has N Messages. The key connecting one or Message to Device is not the Mongo ObjectID but another field. What I want to is to be able to aggregate in on response the Device and it's last 10 most recent received messages. Each message has a receivedAt field. For now I can find this device and aggregate it with the messages it has in messageSchema but I can't figure out a way to limit and order it.
ZehN Device.aggregate([¬ { $match: match },¬ {¬ $lookup: {¬ from: 'messageschemas',¬ localField: 'deviceId',¬ foreignField: 'deviceId',¬ as: 'messages'¬ }¬ }¬ ])
ZehN Sorry. The code above miss the first line which is Device.aggregate([
ZehN and match for now is {}
Omar Hossam
@moar55
Sep 01 2018 09:58
@imjeen do you use express?
Kev
@lineus
Sep 01 2018 11:06
@matrixbot here is a gist with a standalone example that does what I think you're looking for.
imjeen
@imjeen
Sep 01 2018 14:13
@moar55 the Koa
Omar Hossam
@moar55
Sep 01 2018 20:34
use something like this
schema.post('save', function(error, doc, next) {
  if (error.name === 'MongoError' && error.code === 11000) {
    next(new Error({reason: 'duplicate'));
  } else {
    next(new Error({reasons: formatError(error)}); // you can try to format the error object into a standard format 
  }
});
and do the same with schema.post('update',...
then catch this error and pass it to an error handling middleware, if you have that in koa
you can also pass with the error an http status code
Omar Hossam
@moar55
Sep 01 2018 20:40
or use something like this https://github.com/hapijs/boom
I hope this was helpful
Joe Early
@JoeEarly
Sep 01 2018 22:57

Hi all , how should I format my query when I am doing a .find() on the example below, when I want to return all those records that match offerMaker.username

{
    "_id" : ObjectId(""),
    "offerId" : "",
    "offerTitle" : "",
    "offerDesc" : "",
    "offerCondition" : "",
    "offerImage1" : "",
    "offerPostage" : "",
    "offerPrice" : "",
    "offerRequestId" : "",
    "offerMaker" : {
        "_id" : ObjectId(""),
        "name" : "",
        "address1" : "",
        "address2" : "",
        "town" : "",
        "country" : "",
        "countryCode" : "",
        "postzip" : "",
        "shortPostcode" : "",
        "email" : "",
        "username" : "",
        "accounttype" : "",
        "privilege" : "",
        "vat" : "",
        "__v" : 0,
        "resetPasswordExpires" : ISODate(""),
        "resetPasswordToken" : ""
    },
    "__v" : 0
}

Have tried this but to no avail:

 //Find all My Offers
 module.exports.getAllMyOffersListingsPage = function(currentUser,callback) {
  var query = {"offerMaker.username": currentUser};
  Offer.find(query, callback).sort({ addedDate: -1 });
};