0

I am running into an issue where my mongoose model does not seem to recognize all the properties assigned to it when hydrated with data from the db. I am simply trying to access the "make" property of the objects I imported from a CSV.

Model:

const mongoose = require('mongoose'); const productSchema = new mongoose.Schema({ model: String, make: String, keyType: String, years: String, rsType: String, activeRemotesFobs: String, partNumber: String, cost: String }) const Product = mongoose.model('Product', productSchema); module.exports = Product; 

Query to fetch all rows:

exports.index = (req, res) => { Product.find({}, function(err, products){ console.log(products[0]) console.log(products[0].make) res.json(products) }) }; 

And the output from that query:

{ _id: 5a7f2bf4dd2ee45983440017, 'make': 'TOYOTA', model: 'RAV4', keyType: 'STEEL (G/H) KEY', years: '2011-2018', rsType: 'RS ONLY W/FACT. REMOTES + APP', activeRremotesFobs: 'FACT. REMOTES ONLY & APP', partNumber: 'FLRSBA/ASCL6', cost: '$719' } undefined 

So, clearly there is something different about 'make', as I cannot access that property. All the other properties are easily accessible. I imported this using mongoimport, with a headerline that matches my properties. Here is a screenshot of the same record using robomongo, a GUI for mongodb

Record in robomongo

Any help would be appreciated. I have tried renaming the column, changing the order in my schema, and re-importing the records many times with no luck.

9
  • Do you actually call mongoose.model twice and set module.exports twice, or is that a copy/paste error on your post here? Commented Feb 10, 2018 at 17:52
  • 1
    also, what's the output of products[0]['make'] ? Commented Feb 10, 2018 at 17:56
  • @Paul, thats a copy/paste error.. editing now Commented Feb 10, 2018 at 18:34
  • 1
    I just did this and could not repeat your problem. I created a schema identical to yours, saved a product, then pulled it back and consoled it out in the same way, and products[0].make consoled out as Toyota. Are you on the latest version of everything? See if you can repeat the problem in another schema file with a different name. Commented Feb 10, 2018 at 19:50
  • 1
    @Paul - Updating Mongo from 3.2 (Its been a while since I last updated) to 3.4 and then 3.6 fixed it. Not sure what the ultimate problem was... Thanks again for your help Commented Feb 10, 2018 at 21:32

1 Answer 1

1

I also faced this problem many times and I found a workaround to this . You can create a new Object and assign it to the result of query (here products[0]) and then you can access all properties of the second object .

Like

exports.index = (req, res) => { Product.find({}, function(err, products){ const Product2 = Object.assign({},products[0]); console.log(product2) console.log(product2._doc.make) res.json(products) }) }; 
Sign up to request clarification or add additional context in comments.

5 Comments

This might work, but doesn't address the problem. His issue is not repeatable on a different machine, therefore it's not inherently a mongoose/mongodb limitation.
@Paul I have explicitly mentioned that this is a workaround for this type of problem .I still can't figure out the main reason behind these types of problem.
Interestingly, this workaround does not work for me. I still see single quotes around the 'make' key and its still inaccessible. I suspect Paul was on the right track in the comments above with something going haywire in the import.
@sottenad Sorry I had made a typo and didn't write '_doc' try this once
Thanks @Shiv, it turns out upgrading Mongo to the latest stable version fixed this. I appreciate your help

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.