vadimdemedes / mongorito

🍹 MongoDB ODM for Node.js apps based on Redux
1.38k stars 90 forks source link

I can't seem to get find() to return my data #188

Closed Texlo-Dev closed 7 years ago

Texlo-Dev commented 7 years ago

I'm trying to use find() to query a table in my database.

const obj = await tags.where('guildID', msg.guild.id).find()

For some reason, that doesn't return what I'm looking for, It returns this:

Now streaming realtime logs for [2] process
2|encipio  | [ tags {
2|encipio  |     store: 
2|encipio  |      { dispatch: [Function],
2|encipio  |        subscribe: [Function: subscribe],
2|encipio  |        getState: [Function: getState],
2|encipio  |        replaceReducer: [Function: replaceReducer],
2|encipio  |        [Symbol(observable)]: [Function: observable] } },

My table looks like this:

{
    "_id" : ObjectId("596618ccf394244ff3575b0a"),
    "guildID" : "321417443585032203",
    "tagName" : "MongoDB",
    "tagContent" : "test",
    "tagInfo" : {
        "author" : "288855795951599617",
        "createdAt" : ISODate("2017-07-12T12:40:39.437Z"),
        "usage_content" : 1
    },
    "usage_count" : 5
}

Is this an issue with mongorito? I'm pretty sure the query is right.

vadimdemedes commented 7 years ago

find() and other query methods return models, not raw data from MongoDB. In order to get raw data, use:

const tags = Tag
  .find()
  .map(model => model.get()); 
Texlo-Dev commented 7 years ago

@vadimdemedes Hmm For me, that doesn't seem to be a function you sure about that?

vadimdemedes commented 7 years ago

I forgot then(). find() returns a promise.

Texlo-Dev commented 7 years ago

@vadimdemedes So from that promise, model would be returned? And I would map that?

Texlo-Dev commented 7 years ago

Yeah. model.get isn't a function

TypeError: model.get is not a function
2|encipio  |     at tags.find.then.model (/home/richrancy/encipio/commands/taglist.js:12:34)
vadimdemedes commented 7 years ago

Which version of Mongorito are you using?

Texlo-Dev commented 7 years ago

@vadimdemedes Version 3.0.3 Latest I assume

vadimdemedes commented 7 years ago

Could you paste the exact code you're using?

Texlo-Dev commented 7 years ago

@vadimdemedes

const {
    Database,
    Model
} = require('mongorito')
const connection = new Database('localhost/encipio')
const config = require('../config.json')
connection.connect().then(() => console.log('Connected to MongoDB'))
class tags extends Model {}
connection.register(tags)

exports.run = async (client, msg) => {
 const tag = tags
     .find()
    // .then(model => model.map())
   console.log(tag)
}

Kinda wasn't sure about the model mapping

vadimdemedes commented 7 years ago

Try this:

class Tag extends Model {}
connection.register(Tag);

exports.run = async () => {
  const tags = await Tag.find();
  const response = tags.map(tag => tag.get());
};
Texlo-Dev commented 7 years ago

yep, that worked! Thanks man!