KeystoneJS 从另一个值获取 ObjectID
KeystoneJS get ObjectID from another value
我想使用 slug 路径(键)从类别中获取 ObjectID
这是模型(keystone 生成器的默认模型)
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();
这是我从 mongoshell 得到的
db.getCollection('postcategories').find({"key":"test"})
{
"_id" : ObjectId("5853a502455e60d5282d9325"),
"key" : "test",
"name" : "test",
"__v" : 0
}
这只是为了检查密钥是否有效
但是当我在路线上使用它时
var gc = "test";
var c = keystone.list('PostCategory').model.find().where('key').equals(gc);
c.key = gc;
console.log(gc ,c.id);
日志显示测试未定义。
我也尝试使用 postcategories,但它说 keystone 无法识别它
var gc = test; ?????
显然没有定义测试。从 Javascript 的角度来看。
JS 期望 test 是一个变量。而且它当时不知道什么是数据库。
让我们从您的 keystone issue 此处获取我们的讨论。
原因应该是find()
returns一个Promise,所以操作是运行异步的。因此,在您记录该值的那一刻,它仍然是 undefined
.
keystone 演示站点上有 promise 的示例,例如 here。
我想你想要的是这样的:
var gc = "test";
var c = keystone.list('PostCategory').model.find().where('key').equals(gc).exec(function (err, results) {
if (err) {
console.log(err);
} else {
console.log(results);
}
});
或
keystone
.list('PostCategory')
.model
.find()
.where('key')
.equals(gc)
.then(function(category) {
console.log(category.id);
});
此外,我在这里不确定,但如果 find({"key":"test"})
在 mongoshell 中工作,它也可能在 mongoose 中工作,所以你试过 keystone.list('PostCategory').model.find({"key":"test"}).exec(...)
了吗?
我想使用 slug 路径(键)从类别中获取 ObjectID
这是模型(keystone 生成器的默认模型)
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();
这是我从 mongoshell 得到的
db.getCollection('postcategories').find({"key":"test"})
{
"_id" : ObjectId("5853a502455e60d5282d9325"),
"key" : "test",
"name" : "test",
"__v" : 0
}
这只是为了检查密钥是否有效
但是当我在路线上使用它时
var gc = "test";
var c = keystone.list('PostCategory').model.find().where('key').equals(gc);
c.key = gc;
console.log(gc ,c.id);
日志显示测试未定义。
我也尝试使用 postcategories,但它说 keystone 无法识别它
var gc = test; ?????
显然没有定义测试。从 Javascript 的角度来看。 JS 期望 test 是一个变量。而且它当时不知道什么是数据库。
让我们从您的 keystone issue 此处获取我们的讨论。
原因应该是find()
returns一个Promise,所以操作是运行异步的。因此,在您记录该值的那一刻,它仍然是 undefined
.
keystone 演示站点上有 promise 的示例,例如 here。
我想你想要的是这样的:
var gc = "test";
var c = keystone.list('PostCategory').model.find().where('key').equals(gc).exec(function (err, results) {
if (err) {
console.log(err);
} else {
console.log(results);
}
});
或
keystone
.list('PostCategory')
.model
.find()
.where('key')
.equals(gc)
.then(function(category) {
console.log(category.id);
});
此外,我在这里不确定,但如果 find({"key":"test"})
在 mongoshell 中工作,它也可能在 mongoose 中工作,所以你试过 keystone.list('PostCategory').model.find({"key":"test"}).exec(...)
了吗?