These are chat archives for Automattic/mongoose

9th
Oct 2018
Joe Early
@JoeEarly
Oct 09 2018 09:17

@lineus sorryf or the delay in response.

const OfferSchema = mongoose.Schema({
  offerId: {
    type: String,
    index: true
  },
  offerTitle: {
    type: String
  },
  offerDesc: {
    type: String
  },
  offerCondition: {
    type: String
  },
  offerImage1: {
    type: String
  },
  offerImage2: {
    type: String
  },
  offerImage3: {
    type: String
  },
  offerPostage: {
    type: String
  },
  offerPrice: {
    type: String
  },
  offerRequestId: {
    type: String
  },
  offerStatus: {
    type: String
  },
  offerMaker: {
    type: Object
  },
  offerAddedDate: {
    type: Date
  }
})

The above is my Schema modal.

Kev
@lineus
Oct 09 2018 09:18
@JoeEarly when you look at the document in the collection that you are expecting your query to match, is the offerId stored as a string?
Joe Early
@JoeEarly
Oct 09 2018 09:19
image.png
Yea it is indeed
Kev
@lineus
Oct 09 2018 09:21
what is the value of offerId in your Offer.findOne() above?
Joe Early
@JoeEarly
Oct 09 2018 09:27
I am looking for offerId-2df19fa0-c63b-11e8-b20e-650d7fd48660
And I pass in that value into the find but it returns nothing, i then search for it in the Robo 3t tool and its there
Kev
@lineus
Oct 09 2018 09:31

I am looking for offerId-2df19fa0-c63b-11e8-b20e-650d7fd48660

so if you call console.dir(offerId) right before that call to Offer.findOne() you see the string 'offerId-2df19fa0-c63b-11e8-b20e-650d7fd48660'?
apologies if this seems pedantic, I'm just trying to be explicit so I can more easily wrap my head around what's going on :smile:

Joe Early
@JoeEarly
Oct 09 2018 09:34
image.png
my eyes tell me yes lol i hope yours done as well
Kev
@lineus
Oct 09 2018 09:37
yeah, I just compared them from the images, they look the same. can you share your call to compile the schema into a model ( e.g. const Offer = mongoose.model('offer', OfferSchema) )
Joe Early
@JoeEarly
Oct 09 2018 09:38
const Offer = (module.exports = mongoose.model("Offer", OfferSchema))
I have loads of other searchs working within this modal just not this one
Kev
@lineus
Oct 09 2018 09:40
and are you sure that your connecting to a db called iwantfishing from mongoose?
just ruling out the obvious stuff :)
if you're sure that you are connected to the right db and are searching in the right collection, both of which appear to be true, one more thing you could try is to do a findOne on the same model with the _id as the query condition instead, and see if you get a result.
Joe Early
@JoeEarly
Oct 09 2018 09:42
I am indeed :)
Kev
@lineus
Oct 09 2018 09:43
from mongoose
Joe Early
@JoeEarly
Oct 09 2018 09:43
image.png
Kev
@lineus
Oct 09 2018 09:44
do you have any indexes defined for the offerId field?
I'll try and replicate
Joe Early
@JoeEarly
Oct 09 2018 09:44
Dont believe so no
oh i do its set to true
const OfferSchema = mongoose.Schema({
  offerId: {
    type: String,
    index: true
  },
Kev
@lineus
Oct 09 2018 09:45
duh, hehe. I could have looked at your schema!
Joe Early
@JoeEarly
Oct 09 2018 09:46
lol duh here too, I wrote the dam thing
:)
Kev
@lineus
Oct 09 2018 09:51
I tried to replicate it in this gist but the query works for me. I'll have to give this some more thought while I'm trying to coax my teenagers out of bed for school :smile:
Joe Early
@JoeEarly
Oct 09 2018 09:54
ummm, Ill have to strip mine down without the callback and see where it leads me, hopefully somewhere other than the rabbit hole. Thanks for taking a look
Kev
@lineus
Oct 09 2018 09:55
I always do that - default to promises - I'll rewrite my example using callbacks too.
what does your callback look like @JoeEarly ?
Joe Early
@JoeEarly
Oct 09 2018 09:58
undefined
it should be the found document
Like I have another example that returns all offers and it works fine.
module.exports.getAllOffersListingsPage = function(callback) {
  const query = {}
  Offer.find(query, callback).sort({ addedDate: -1 })
}
Kev
@lineus
Oct 09 2018 10:00
what are you passing in there as the value of callback?
Joe Early
@JoeEarly
Oct 09 2018 10:00
Even if I change my one that doesnt work to a find() and not a findOne, i still get undefined
Since that one above find(), I dont pass in anything, I just call it, and get All the docs it can find.
Kev
@lineus
Oct 09 2018 10:02
are you accounting for the fact that the first parameter to mongoose query callbacks is an error? the parameter sig is (error, result)
Joe Early
@JoeEarly
Oct 09 2018 10:03
Ummm so I should have an error and a callback/result on all of these?
Kev
@lineus
Oct 09 2018 10:04
this gist uses a callback instead, hopefully to clarify what I mean.
Kev
@lineus
Oct 09 2018 10:17
@JoeEarly I was able to reproduce what you're seeing. I'll see if I can figure it out right quick :)
Joe Early
@JoeEarly
Oct 09 2018 10:19
Aww nice, so maybe not my code but rather a mongoose problem?
(As am sure oyu have guessed, am learning slowly lol )
Kev
@lineus
Oct 09 2018 10:21
actually, it's a me problem :) When I changed my example to more closely match yours, I used Model.find() and didn't account for the fact that find returns an array instead of the doc.
once I changed my assertion to assert.strictEqual(found[0].name, 'testing123'); it works for me
Joe Early
@JoeEarly
Oct 09 2018 10:22
what about findOne ?
I have to go make my kid's lunches back in 30ish mins. Sorry I haven't been able to help so far @JoeEarly
Joe Early
@JoeEarly
Oct 09 2018 10:26

god dam it lol what am i doing wrong lol just tryed a normal find and got nothing.

No worries, thanks so far

Kev
@lineus
Oct 09 2018 11:58

@JoeEarly when you said

Since that one above find(), I dont pass in anything, I just call it, and get All the docs it can find.

did you mean you were not passing in a callback? or a query?

what I meant earlier was , when you call getAllOffersListingsPage(callback) what is the value of callback?
Joe Early
@JoeEarly
Oct 09 2018 12:38

The value is that case is an object contain the documents from mongo.

I got mine going in the end. Few tweaks and it seemed to start working lol not that I can pin point still what I was doing wrong.

Finally have it updating my offer in the Db, now just to test the images bit

Thanks again for your support @lineus
Kev
@lineus
Oct 09 2018 12:39
always a pleasure! Happy Fishing