Reputation: 2646
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
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.
Upvotes: 0
Views: 873
Reputation: 1114
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)
})
};
Upvotes: 1