ga-wdi-boston / team-project

Other
2 stars 39 forks source link

Getting Reference Data from Mongo/Mongoose #346

Closed slammyde7113 closed 7 years ago

slammyde7113 commented 7 years ago

So I'm trying to query my reference data in my database, where a Cart has an owner and a product id. Using the product id, I'm trying to query the values from the database.Here's what I've done so far:

const show = (req, res) => {
  console.log(req.cart.product)
  console.log(Product.find({ _id: '5951976290fe850d650ae4c1'}))
  res.json({
    cart: req.cart.toJSON({ virtuals: true, user: req.user })

  })
}

But i keep getting an empty return from the Product table query:

Query {
  _mongooseOptions: {},
  mongooseCollection: 
   NativeCollection {
     collection: Collection { s: [Object] },
     opts: { bufferCommands: true, capped: false },
     name: 'products',
     collectionName: 'products',
     conn: 
      NativeConnection {
        base: [Object],
        collections: [Object],
        models: [Object],
        config: [Object],
...
...
 _castError: null,
  _count: [Function],
  _execUpdate: [Function],
  _find: [Function],
  _findOne: [Function],
  _findOneAndRemove: [Function],
  _findOneAndUpdate: [Function],
  _replaceOne: [Function],
  _updateMany: [Function],
  _updateOne: [Function] }

Is there something wrong with my query?

benjimelito commented 7 years ago

Are you meaning to return a user, or a product?

slammyde7113 commented 7 years ago

I'm trying to mimic what the serializer in ruby. Its returning a cart but instead of showing the product id I want it to show the attributes of the product in the cart

slammyde7113 commented 7 years ago

and changing the req.cart to req.cart.product only returns the product id

slammyde7113 commented 7 years ago

sad face

benjimelito commented 7 years ago

Gotcha, sorry I was a little confused earlier. So I would probably try getting that id, and then Finding the product that you want by using that ID, and then returning that data.

benjimelito commented 7 years ago

Are you getting the data you expect when console.logging the results of Product.find?

slammyde7113 commented 7 years ago

No I get some long janky empty object query I modified my code with the following:

const show = (req, res) => {
  console.log(Product.find({},function(err,models){
        console.log(models)
         if (err) {
            res.render('error', {
                status: 500
            })
        } else {
            res.jsonp(models)
        }
    }))
  // // console.log(Product.find({id: '5951976290fe850d650ae4c1'}))
  // res.json({
  //   cart: req.cart.product.toJSON({ virtuals: true, user: req.user })
  //
  // })
}

and this is the response:

Query {
  _mongooseOptions: {},
  mongooseCollection: 
   NativeCollection {
     collection: Collection { s: [Object] },
     opts: { bufferCommands: true, capped: false },
     name: 'products',
     collectionName: 'products',
     conn: 
      NativeConnection {
        base: [Object],
        collections: [Object],
        models: [Object],
        config: [Object],
        replica: false,
        hosts: null,
        host: 'localhost',
        port: 27017,
        user: undefined,
        pass: undefined,
        name: 'nozama-development',
        options: [Object],
        otherDbs: [],
        _readyState: 1,
        _closeCalled: false,
        _hasOpened: true,
        _listening: false,
        db: [Object] },
     queue: [],
     buffer: false,
     emitter: 
      EventEmitter {
        domain: null,
        _events: {},
        _eventsCount: 0,
        _maxListeners: undefined } },
  model: 
   { [Function: model]
     hooks: Kareem { _pres: {}, _posts: {} },
     base: 
      Mongoose {
        connections: [Object],
        models: [Object],
        modelSchemas: [Object],
        options: [Object] },
     modelName: 'Product',
     model: [Function: model],
     db: 
      NativeConnection {
        base: [Object],
        collections: [Object],
        models: [Object],
        config: [Object],
        replica: false,
        hosts: null,
        host: 'localhost',
        port: 27017,
        user: undefined,
        pass: undefined,
        name: 'nozama-development',
        options: [Object],
        otherDbs: [],
        _readyState: 1,
        _closeCalled: false,
        _hasOpened: true,
        _listening: false,
        db: [Object] },
     discriminators: undefined,
     _events: { init: [Function], save: [Function] },
     _eventsCount: 2,
     schema: 
      Schema {
        obj: [Object],
        paths: [Object],
        aliases: {},
        subpaths: {},
        virtuals: [Object],
        singleNestedPaths: {},
        nested: {},
        inherits: {},
        callQueue: [Object],
        _indexes: [],
        methods: {},
        statics: {},
        tree: [Object],
        query: {},
        childSchemas: [],
        plugins: [Object],
        s: [Object],
        options: [Object],
        '$globalPluginsApplied': true },
     collection: 
      NativeCollection {
        collection: [Object],
        opts: [Object],
        name: 'products',
        collectionName: 'products',
        conn: [Object],
        queue: [],
        buffer: false,
        emitter: [Object] },
     Query: { [Function] base: [Object] },
     '$__insertMany': [Function],
     insertMany: [Function] },
  schema: 
   Schema {
     obj: { name: [Object], price: [Object], description: [Object] },
     paths: 
      { name: [Object],
        price: [Object],
        description: [Object],
        _id: [Object],
        __v: [Object] },
     aliases: {},
     subpaths: {},
     virtuals: { id: [Object] },
     singleNestedPaths: {},
     nested: {},
     inherits: {},
     callQueue: [ [Object], [Object], [Object], [Object], [Object], [Object] ],
     _indexes: [],
     methods: {},
     statics: {},
     tree: 
      { name: [Object],
        price: [Object],
        description: [Object],
        _id: [Object],
        id: [Object],
        __v: [Function: Number] },
     query: {},
     childSchemas: [],
     plugins: [ [Object], [Object], [Object] ],
     s: { hooks: [Object], kareemHooks: [Object] },
     options: 
      { toJSON: [Object],
        retainKeyOrder: false,
        typeKey: 'type',
        id: true,
        noVirtualId: false,
        _id: true,
        noId: false,
        validateBeforeSave: true,
        read: null,
        shardKey: null,
        autoIndex: null,
        minimize: true,
        discriminatorKey: '__t',
        versionKey: '__v',
        capped: false,
        bufferCommands: true,
        strict: true,
        pluralization: true },
     '$globalPluginsApplied': true },
  op: 'find',
  options: { retainKeyOrder: false },
  _conditions: {},
  _fields: undefined,
  _update: undefined,
  _path: undefined,
  _distinct: undefined,
  _collection: 
   NodeCollection {
     collection: 
      NativeCollection {
        collection: [Object],
        opts: [Object],
        name: 'products',
        collectionName: 'products',
        conn: [Object],
        queue: [],
        buffer: false,
        emitter: [Object] },
     collectionName: 'products' },
  _traceFunction: undefined,
  _castError: null,
  _count: [Function],
  _execUpdate: [Function],
  _find: [Function],
  _findOne: [Function],
  _findOneAndRemove: [Function],
  _findOneAndUpdate: [Function],
  _replaceOne: [Function],
  _updateMany: [Function],
  _updateOne: [Function] }
[ { _id: 5952b57ea52d092b8d34c6b0,
    name: 'test00000',
    price: 0,
    description: 'test',
    __v: 0 } ]

Its a ton of stuff but I really only need the bottom array but I dont know how to only get that piece

slammyde7113 commented 7 years ago
const show = (req, res) => {
  Product.find({},function(err,models){
        console.log(models)
         if (err) {
            res.sendStatus(500)
        } else {
            res.json(models)
        }
    })
}

Solved