I want to get the ObjectID from a category by using slug path (key)
this is the model (its the default from keystone generator)
var keystone = require('keystone');
/**
* PostCategory Model
* ==================
*/
var PostCategory = new keystone.List('PostCategory', {
autokey: { from: 'name', path: 'key', unique: true },
});
PostCategory.add({
name: { type: String, required: true },
});
PostCategory.relationship({ ref: 'Post', path: 'categories' });
PostCategory.register();
Heres what i get from mongoshell
db.getCollection('postcategories').find({"key":"test"})
{
"_id" : ObjectId("5853a502455e60d5282d9325"),
"key" : "test",
"name" : "test",
"__v" : 0
}
This is just to find if the key works
but when i use this on routes
var gc = "test";
var c = keystone.list('PostCategory').model.find().where('key').equals(gc);
c.key = gc;
console.log(gc ,c.id);
The log says test undefined.
i tried to use postcategories as well but it says keystone doesnt recognize it
Let's pick up our discussion from your keystone issue here.
The reason should be that
find()
returns a Promise, so the operation is ran asynchronously. Therefore, at the moment you log the value, it's stillundefined
.There are examples of the promises on the keystone demo site, for example here.
I think what you want is something like:
or
Also, I'm not sure here but if
find({"key":"test"})
works in mongoshell, it might also work in mongoose, so have you triedkeystone.list('PostCategory').model.find({"key":"test"}).exec(...)
?