These are chat archives for Automattic/mongoose

9th
Mar 2018
Prasad nv
@prasad47
Mar 09 2018 09:37
HI All
I need Bit help ..
save method is not working with updated mongoose version on my node project
i raised the issue on github mongoose they refer to gitter to chat
i am testing my luck Folks
the above thing is my issue .. could any one take a look and help me ..
I am totally stucked up in saving Document to mongodb via mongoose
Kev
@lineus
Mar 09 2018 09:42
hi @prasad47 I'm trying to replicate the issue now. I haven't previously used mongoose 3.x so it might take me a little while to get everything setup.
Prasad nv
@prasad47
Mar 09 2018 09:45
Thank you so much Kev !!
please let me know if you need any thing else from my side so i can update in issue ticket
Kev
@lineus
Mar 09 2018 09:46
what version of mongod are you running?
Prasad nv
@prasad47
Mar 09 2018 09:49
We are not using any mongo help from external npm package
"mongodb": "3.0.1", from mongoose latest plug in
internal dependency
Kev
@lineus
Mar 09 2018 09:53
I mean what is the mongodb version on the server where you are connecting mongoose
Prasad nv
@prasad47
Mar 09 2018 09:57
3.4.6
Kev
@lineus
Mar 09 2018 10:25
is this code in a publicly accessible repo? I'm trying to figure this out by what's in your comment but there are some gaps, like the contents of your schema, that might give some insight.
@prasad47 ^
Prasad nv
@prasad47
Mar 09 2018 10:30
i am sorry kev this is private repository
Kev
@lineus
Mar 09 2018 10:30
I understand :)
Prasad nv
@prasad47
Mar 09 2018 10:30
:-)
do you want me to update the shcema or something
Kev
@lineus
Mar 09 2018 10:31
yeah, if you can share that part it might help. you can just update that comment in the issue
Prasad nv
@prasad47
Mar 09 2018 10:31
ok i can update the schema @ how we defined ...
in few minutes
Prasad nv
@prasad47
Mar 09 2018 10:37
Hi Kev i just commented Schema in ticket
pls take a look
Kev
@lineus
Mar 09 2018 10:42
cool, thanks. I have another question, if these docs were saved with mongoose 3.8.8 that should mean that at some point you also upgraded your mongodb server as well right? what version where you on before?
q1240641788
@q1240641788
Mar 09 2018 10:43
blob
how can i get this element on mongoose
i try to use object.key and object[key],but get nothing
Kev
@lineus
Mar 09 2018 10:47
@q1240641788 in what context are you trying to access that path? after a Model.find() or findOne() or directly from the model instance before you save it, or in the callback from .save()?
q1240641788
@q1240641788
Mar 09 2018 10:50
model.find()
in that callback
Kev
@lineus
Mar 09 2018 10:51
model.find() returns an array of results, even if there's only one. you'll have to loop through it, use [0], or modify your query to look for one doc.
q1240641788
@q1240641788
Mar 09 2018 10:54
sorry ,i used model.findById
i fogeted
another value all of them i can get
but this one i cant
Kev
@lineus
Mar 09 2018 10:55
try putting quotes around the key authorizer_appid in your query
q1240641788
@q1240641788
Mar 09 2018 10:57
ok,i will try it soon,thanks very much
Kev
@lineus
Mar 09 2018 11:06
@q1240641788 Let me make sure I understand your situation:
you saved at least one document with a path called 'authorizer_appid' to a collection in mongodb
then you searched the collection for that document using Model.findById(someDoc._id, callback(err, doc) {*} )
then when you try to access the path 'authorizer_appid' on the resulting doc in the callback doc.authorizer_appid you don't see a value.
is that right? what do you see when you console.log(doc.authorizer_appid)?
Prasad nv
@prasad47
Mar 09 2018 11:08
@key i will come back to you with that answers .. in some time
Kev
@lineus
Mar 09 2018 11:08
@prasad47 I forgot to add your name when I asked this above: if these docs were saved with mongoose 3.8.8 that should mean that at some point you also upgraded your mongodb server as well right? what version where you on before?
oh
ok
sorry, I hit return with reading your comment
Prasad nv
@prasad47
Mar 09 2018 11:09
Np i can check and get back to you with that answers @key
Kev
@lineus
Mar 09 2018 11:09
thanks man, I'll keep researching this on my end, hopefully someone who has been around longer can jump in and lend some help as well.
q1240641788
@q1240641788
Mar 09 2018 11:16
@lineus i used console before,it Is undefined
Kev
@lineus
Mar 09 2018 11:17
and if you console.log(doc) do you see the property then?
q1240641788
@q1240641788
Mar 09 2018 11:17
yeah
i can
Kev
@lineus
Mar 09 2018 11:18
what is the schemaType for that path?
q1240641788
@q1240641788
Mar 09 2018 11:18
String
i think something wrong with the name
maybe
Kev
@lineus
Mar 09 2018 11:20
let me create a test real quick, what version of mongoose are you using?
q1240641788
@q1240641788
Mar 09 2018 11:20
the last version on npm
Kev
@lineus
Mar 09 2018 11:21
cool, just a couple mins, brb
q1240641788
@q1240641788
Mar 09 2018 11:22
okay
whats your country
Kev
@lineus
Mar 09 2018 11:27
I'm in the U.S., you?
q1240641788
@q1240641788
Mar 09 2018 11:27
china ,haha
Kev
@lineus
Mar 09 2018 11:28
nice :)
q1240641788
@q1240641788
Mar 09 2018 11:29
do u use another message program
Kev
@lineus
Mar 09 2018 11:30
here's a gist that shows my test
q1240641788
@q1240641788
Mar 09 2018 11:30
what
Kev
@lineus
Mar 09 2018 11:31
I'm also on the mongoose slack channel and the #node and #javascript irc channels.
click on the green word gist above and that shows a script creating a doc in the db with that field name and the output.txt shows that my test can access that field by that name. if you want to create a gist of your code or point me to the public repo if there is one I can take a look at why it's not working for you.
I have to get my kids to school, back in an hour :)
q1240641788
@q1240641788
Mar 09 2018 11:34
all right,thanks man
Kev
@lineus
Mar 09 2018 11:45
@q1240641788 one last question before I go, if you look at that specific document in the mongo shell, do you see a string stored in that path?
Prasad nv
@prasad47
Mar 09 2018 11:46
@kev i am back
actually the version we are using on server is 3.4.6
mongo 3.4.6
q1240641788
@q1240641788
Mar 09 2018 11:47
@lineus yes
Prasad nv
@prasad47
Mar 09 2018 11:47
persisting it now
q1240641788
@q1240641788
Mar 09 2018 11:48
@lineus i also see that value
Prasad nv
@prasad47
Mar 09 2018 11:48
@lineus you there ?
message: 'No matching document found for id "5aa253cf91cff8001387748b"',
name: 'VersionError' }
i can able to see this document present in db
is this kind of version mismatch
?
@lineus is it possible for you for screeshare i mean can you available for voice chat on skype
it may sorted quicly on voice call i feel
Kev
@lineus
Mar 09 2018 11:57
@prasad47 I have to get my kids off to school, I only have a couple of minutes before I have to get back to them. I tried connecting mongoose@3.8.8 with a mongodb server 3.4.4 and another one running 3.0.2 and in both cases I got an error from the native driver complaining about the server version. the compatibility chart here shows for mongodb server 3.4.6 you'd have to be using mongoose 4.7.3 or 5. so not sure how you got around that.
Kev
@lineus
Mar 09 2018 12:53
I'm back @prasad47. I'm not in a position to jump on a call at the moment, my wife and I are on different schedules and if I wake her up I'm a dead man :)
Kev
@lineus
Mar 09 2018 13:07
@prasad47 can you open a mongo shell to the mongodb server in question and type db.version() and put the server version here?
something like mongo --host <hostname> --port <portnumber> -u <username> -p <password> --authenticationDatabase <db> <db> with the correct values should get you there.
sorry I just remembered that at least some of your logs in the github issue were from windows, I'm not sure if that command will work as is on windows, you might have to adjust it for your setup.
Prasad nv
@prasad47
Mar 09 2018 15:42
titled.jpg
hi
hi @lineus iam back i just posted the screen shot plz take a look
Prasad nv
@prasad47
Mar 09 2018 15:48
@lineus :-) :-) :-) sure i understand Lineus :-) .. i just posted a comment on my thread
Kev
@lineus
Mar 09 2018 15:54
I just wanted to confirm @prasad47, you said this code worked with mongoose@3.8.8, didn't you?
Prasad nv
@prasad47
Mar 09 2018 15:56
yes it is @lineus
Kev
@lineus
Mar 09 2018 15:57

and the purpose of this

var order = new xyz(_.extend({obj:obj});

   order .save(function(err) {
       if (err) {
           callback(err);
       } else {
           result=order;
      }

is that to create a new model instance with a copy of the return from a Model.findOne() or .... what is obj in that context?

Prasad nv
@prasad47
Mar 09 2018 15:58
ok let me check
Kev
@lineus
Mar 09 2018 15:58
and what is xyz as well, it looks like it might be Model()?
Prasad nv
@prasad47
Mar 09 2018 15:59
Actually var order = new order(_.extend{obj:obj});
then we save it to the object by save method
to db
which is working with 3.8.8 due to some very forcing consequneces i updated the project to 5.0.9
where find,findOne working
but .save it eh one not working ..
Kev
@lineus
Mar 09 2018 16:01
oh, ok. I just copied that snippet above from the issue on github.
is it by chancevar order = new Order(... ? because var order = new order(... seems wrong.
where is the right hand side defined? and is that something like var Order = mongoose.model(..?
Prasad nv
@prasad47
Mar 09 2018 16:05
Actaully .. let me extend it
var mongoose =require('mongoose'),Order = mongoose.model('Order');
var order = new Order(...)
this is how we are inheriting and creating from the model @lineus
Kev
@lineus
Mar 09 2018 16:10
ok, can you change the line where you call callback(err) in the save callback to return console.error('can you see me now') and run it again. Tell me if the error changes or if it stays the same as before.
Prasad nv
@prasad47
Mar 09 2018 16:11
ok
Prasad nv
@prasad47
Mar 09 2018 16:18
let me re run Sorry
@lineus i just changed console.warn to .error
and then it started consoling console.error('can you see me now')
but it is not returning it !!
Kev
@lineus
Mar 09 2018 16:23
by not returning it, do you mean that the terminal hangs and you have to ^c to get back control?
Prasad nv
@prasad47
Mar 09 2018 16:24
yes the endpoint hangs
not terminal i mean the method failing to return the console.error stuff as promise
Kev
@lineus
Mar 09 2018 16:27
what is the first line of text printed to the console when you run the code with the 'can you see me' in the save callback?
also, in your error message from version 5.0.9, it points to orders.server.controller.js:84:9 . what's on line 84 of that file?
Prasad nv
@prasad47
Mar 09 2018 16:30
Actually it is can you see me now is the first line
and 84 th line is obj.save(function(err) {
Kev
@lineus
Mar 09 2018 16:31
ok, so the order.save() is probably on line 84 then...
Prasad nv
@prasad47
Mar 09 2018 16:31
yes obj.save(function(err) { is the line
Kev
@lineus
Mar 09 2018 16:32
ok, you can put your old callback line back. I just wanted to make sure it was erroring out at that point.
just curious, is this in production or is it a dev environment
Prasad nv
@prasad47
Mar 09 2018 16:33
actually i stopped this code from production for my Job Sake :-)
Kev
@lineus
Mar 09 2018 16:34
oh good hehe
Prasad nv
@prasad47
Mar 09 2018 16:34
it is development fortunately
:-)
Kev
@lineus
Mar 09 2018 16:37
can you inspect the value that you're calling new Order on? that (lodash?) call to extend is perplexing in this context.
from what I read .extend is just an alias for .assignIn which makes a copy of the object including it's own and inherited properties. I'm not sure what that gets you in the context of creating a new model instance.
unless that isn't lodash, in which case I read the wrong docs :)
Prasad nv
@prasad47
Mar 09 2018 16:41
it is a kind of ordering so we need to reuse code which leaded to usability with .extend
ok inspecting
Prasad nv
@prasad47
Mar 09 2018 16:47
it is a JSON object @lineus
Kev
@lineus
Mar 09 2018 17:00
and does that object conform to the schema for that model?
@prasad47 ^
Prasad nv
@prasad47
Mar 09 2018 17:00
yes preciesly
I Like to give a try with the version upgrade Lineus
i have to give a try with that .. which i requested the folks to do it ..
it may get rid the error i think
Kev
@lineus
Mar 09 2018 17:01
which version upgrade are you referring to?
Prasad nv
@prasad47
Mar 09 2018 17:02
my mongo server is having 3.4.6
but for mongoose 5.0.9 we may need mongo 3.6.x
as per
i need to give a try ...
Kev
@lineus
Mar 09 2018 17:03
it's worth a shot, but that doc says that 5.x can go all the way back to ver 3.0.x of mongodb Server.
Prasad nv
@prasad47
Mar 09 2018 17:04
yes that is correct ..
but the error is clue less to me ...
no code change only version upgrade & no syntax changes happened as per docs tooo
for new vesrion bit frustrating
Kev
@lineus
Mar 09 2018 17:07

when you create a new instance of your Model, new Order(... that lodash method is sending an object that looks like this

{ obj: { ...fields } }

but I'm not sure that will work with the schema you posted in the github issue. I'm going to test that out with a smaller more manageable example while you try the upgrade.

Prasad nv
@prasad47
Mar 09 2018 17:15
ok lineus
Kev
@lineus
Mar 09 2018 17:18
this is what I would expect, given my albeit incomplete understanding of your example gist here
Prasad nv
@prasad47
Mar 09 2018 17:19
can you do a change in that
const test = new Test(_.extend({ obj: obj })) instead
const test = new Test(_.extend(obj))
obj={properites : properties }
it is precise
Kev
@lineus
Mar 09 2018 17:21
again, I copied that from your example in the github issue.
Prasad nv
@prasad47
Mar 09 2018 17:22
oh ok .. that is fine this is the other way i used to get successed but same issue
both are same no problem ..
but for more meaning i changed to {attr:obj}
Kev
@lineus
Mar 09 2018 17:23
no, both aren't the same
doing it the way it is in the issue with the {attr:} breaks it, without that it works fine.
Prasad nv
@prasad47
Mar 09 2018 17:25
actually i mean {obj:obj} i mean {schema:"my scheam",isOrdered:false,summary:{total:10.00}}
so it is pertaining right
Kev
@lineus
Mar 09 2018 17:31
here is how it works with just _.extends(obj)
keep in mind, I can't see most of your code, or input, so I'm just piecing things together the best I can. that's why I tried to distill an example down to a schema with one required property like yours, a plain object being extended the way yours was in the example, and then saved it as close to the way you are as possible.
grabbing some food, back in a bit :) let me know what you find.
Prasad nv
@prasad47
Mar 09 2018 17:39
is how it works with just _.extends(obj) this is exactly how iam using @lineus
we are on same page :-) sure man .. sorry for confusing you .. but we are same page which is a good thing !!
Dinner time :-)