0

I am trying to load a bunch of data from a file into my mongoDB; Using node: v8.9.1, mongoose: ^4.10.8, MongoDB v3.4.1 - running on Win 10.

only a couple elements from the document are being inserted. I suspect something in the schema, but I cant be sure, the console.log reflects the date 100% correctly.

This is my data: /_mock_data/test.json

[{
    "AUTHOR":"Anthony Johnson",
    "TITLE":"A Little March",
    "COLLECTION_TITLE":"Classic Fairytails for Children",
    "FORMAT":"Trade Paperback",
    "PUBLISHER":"Randon House",
    "SKU":"10055",
    "SUGGESTED_GRADE_LEVEL":[6],
    "STATE_RATING":[{"NY":[1]}, {"PA":[1]}, {"VA":[3]}],
    "AUDIO_LINK":""
},...]

Here's the code: app.js (reduced to what's relevant)

var express = require( 'express' );
var app = express();
var bodyParser = require( 'body-parser' );
var mongoose = require( 'mongoose' );
    mongoose.Promise = require( 'bluebird' );

app.use( bodyParser.json() );

var Catalog = require( './models/catalog' );

mongoose.connect('mongodb://localhost:27017/test', {server: { poolSize: 5 }});
var db = mongoose.connection;

app.get( '/api/loadCatalog', function( req, res ) {
    var catalog = require( './_mock_data/test.json' );

    for ( var i in catalog ) {

        Catalog.create( catalog[i] );
        console.log( catalog[i] );

    };

});

app.listen( 3000, function() {
    console.log( 'server started on port 3000' );
});

/models/catalog

var mongoose = require( 'mongoose' );

// catalog schema
var catalogSchema = mongoose.Schema({
    author : {
        type: String
        ,required: false
    }
    ,title : {
        type: String
        ,required: false
    }
    ,collection_title : {
        type: String
        ,required: false
    }
    ,format : {
        type: String
        ,required: false
    }
    ,publisher : {
        type: String
        ,required: false
    }
    ,sku : {
        type: String
        ,required: false
    }
    ,suggested_grade_level : {
        type: Array
        ,required: false
    }
    ,state_rating : {
        type: [mongoose.Schema.Types.Mixed]
        ,required: false
    }
    ,audio_link : {
        type: String
        ,required: false
    }
});

var Catalog = module.exports = mongoose.model( 'Catalog', catalogSchema );

// Get Items
module.exports.getItems = function( callback, limit ) {
    Catalog.find( callback ).limit( limit );
};

// Add Item
module.exports.addItem = function( item, callback ) {
    Catalog.create( item, callback );
};

and the errors I'm getting -

Unhandled rejection TypeError: Cannot use 'in' operator to search for '_id' in Anthony Johnson
Unhandled rejection TypeError: Cannot use 'in' operator to search for '_id' in A Little March
Unhandled rejection TypeError: Cannot use 'in' operator to search for '_id' in Classic Fairytails for Children
Unhandled rejection TypeError: Cannot use 'in' operator to search for '_id' in Trade Paperback
Unhandled rejection TypeError: Cannot use 'in' operator to search for '_id' in Randon House
Unhandled rejection TypeError: Cannot use 'in' operator to search for '_id' in 10055
Unhandled rejection TypeError: Cannot use 'in' operator to search for '_id' in 6

So the data I get looks like this

{
    "_id" : ObjectId("5a28fa8503511d1d14054c92"),
    "state_rating" : [ ],
    "suggested_grade_level" : [ ],
    "__v" : 0
}

All the string data errored and the object data went in, The "actual" data is an array of 1000's, and this is a one time import, yes I could use mongo cli to do the import, but I am only developing locally, Eventually I'll be shoving this data to an Atlas account, and as far as I can tell, this will be the way to get the data in place.

the console.log inside the loop gives me the actual data

Anthony Johnson
A Little March
Classic Fairytails for Children
Trade Paperback
Randon House
10055
[ 6 ]
[ { NY: [ 1 ] }, { PA: [ 1 ] }, { VA: [ 3 ] } ]

I'm pretty sure Im missing something basic about using the model to use the schema in this way... my "get" and "add" work fine...maybe I should do the loop in the model, not the app...

2
  • You are passing catalog[i]. I think it should include the key with it. like Catalog.create( {your_key:catalog[i]}); Commented Dec 8, 2017 at 10:27
  • yes Kiran, Thank you - I see that it is unclear the way i posted it - there should be an array around the object, as the file is an array. I have fixed that. Commented Dec 9, 2017 at 6:05

1 Answer 1

1

You need to make a small change that loop,

   for ( var i in catalog ) {
    var key = convertStringToCamelCase(i);
    Catalog.create( {key : catalog[i]} );
    console.log( {key : catalog[i]} );

    };

To convert the key to lower and camel case to match with the schema, you can create a small helper function like this

function convertStringToCamelCase(str_original){
    var str = str_original.toLowerCase();
    return str.split(' ').map(function(item, index){
        return index !== 0 
            ? item.charAt(0).toUpperCase() + item.substr(1) 
            : item.charAt(0).toLowerCase() + item.substr(1);
    }).join('');

}

Sign up to request clarification or add additional context in comments.

1 Comment

Kiran, Thank you - I was half curious if there was a case sensitivity thing going on, I have been spoiled (or ruined) by working in a case-less language for close to 20 years, and it still catches me. I changed the case on the keys in the file to match the case in the schema - works fine.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.