我正在使用 strapi 构建一个 API 但 findone returns 总是{}
I'm using strapi to build an API but findone returns always {}
这是我的代码
console.log(JSON.stringify(receivedProduct));
var dbProduct = strapi.query('product').findOne({ wooSku: receivedProduct.sku });
console.log(JSON.stringify(dbProduct));
收到产品输出:
{
...some json...
"sku": "LUPV-03300-1",
...some json...
}
db产品输出:
{}
如果我在后台查看,已经存在具有此属性值的产品:
有什么问题吗?
###############################
更新:
作为一个承诺,它 returns 使用 .then() ...
但是我不明白为什么在添加函数调用后它是“undefined”
var dbProduct = strapi.query('product').findOne({ wooSku: receivedProduct.sku })
.then(dbProduct => {
console.log(JSON.stringify(dbProduct));
saveProductIfNotExists(receivedProduct);}
);
可以考虑这种做法
`strapi.query('modelName', 'pluginName')
在此thread the issue is addressed
根据用户
the findOne means to find one by it's ID. If you need to filter for
anything else I would recommend you use find as that accepts all the
other filters.
我的第一个猜测是您收到了待处理的承诺,因为这看起来像是一个异步任务。然后我看了最后一个例子,他们使用 await
https://strapi.io/documentation/3.0.0-beta.x/concepts/queries.html#custom-queries 是的,这可能是一个承诺。你可以试试这个:
console.log(JSON.stringify(receivedProduct));
var dbProduct = strapi.query('product').findOne({ wooSku: receivedProduct.sku }).then(dbProduct => {
console.log(JSON.stringify(dbProduct));
});
这是我的代码
console.log(JSON.stringify(receivedProduct));
var dbProduct = strapi.query('product').findOne({ wooSku: receivedProduct.sku });
console.log(JSON.stringify(dbProduct));
收到产品输出:
{
...some json...
"sku": "LUPV-03300-1",
...some json...
}
db产品输出:
{}
如果我在后台查看,已经存在具有此属性值的产品:
有什么问题吗?
###############################
更新:
作为一个承诺,它 returns 使用 .then() ... 但是我不明白为什么在添加函数调用后它是“undefined”
var dbProduct = strapi.query('product').findOne({ wooSku: receivedProduct.sku })
.then(dbProduct => {
console.log(JSON.stringify(dbProduct));
saveProductIfNotExists(receivedProduct);}
);
可以考虑这种做法
`strapi.query('modelName', 'pluginName')
在此thread the issue is addressed
根据用户
the findOne means to find one by it's ID. If you need to filter for anything else I would recommend you use find as that accepts all the other filters.
我的第一个猜测是您收到了待处理的承诺,因为这看起来像是一个异步任务。然后我看了最后一个例子,他们使用 await
https://strapi.io/documentation/3.0.0-beta.x/concepts/queries.html#custom-queries 是的,这可能是一个承诺。你可以试试这个:
console.log(JSON.stringify(receivedProduct));
var dbProduct = strapi.query('product').findOne({ wooSku: receivedProduct.sku }).then(dbProduct => {
console.log(JSON.stringify(dbProduct));
});