Closed slammyde7113 closed 7 years ago
Are you meaning to return a user, or a product?
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
and changing the req.cart to req.cart.product only returns the product id
sad face
Gotcha, sorry I was a little confused earlier. So I would probably try getting that id, and then Find
ing the product that you want by using that ID, and then returning that data.
Are you getting the data you expect when console.log
ging the results of Product.find
?
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
const show = (req, res) => {
Product.find({},function(err,models){
console.log(models)
if (err) {
res.sendStatus(500)
} else {
res.json(models)
}
})
}
Solved
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:
But i keep getting an empty return from the Product table query:
Is there something wrong with my query?