gpt4 book ai didi

javascript - "Your filters contain a field ' 状态 ' that doesn' t 出现在您的模型定义中,也不出现 's relations"(Strapi)

转载 作者:行者123 更新时间:2023-12-05 00:33:10 24 4
gpt4 key购买 nike

https://strapi.io/documentation/v3.x/guides/draft.html#apply-our-changes
我正在自定义我自己的 API,当然我想只过滤状态为已发布的帖子,所以我按照上面的文档来查看它是如何工作的。
我实际上使用了除了我自己的模型之外的确切代码,所以我的代码如下

'use strict';
const { sanitizeEntity } = require('strapi-utils');

/**
* Read the documentation (https://strapi.io/documentation/v3.x/concepts/controllers.html#core-controllers)
* to customize this controller
*/

module.exports = {
async find(ctx) {
let entities;

console.log(ctx.query, 'before');
ctx.query = {
...ctx.query,
status: 'published',
};

console.log(ctx.query, 'after');
if (ctx.query._q) {
entities = await strapi.services.post.search(ctx.query);
} else {
entities = await strapi.services.post.find(ctx.query);
}

return entities.map(entity => sanitizeEntity(entity, { model: strapi.models.post }));
}
};
然后我做一个正常的get API调用 localhost:1337/posts但我得到了这个错误
{
"statusCode": 400,
"error": "Bad Request",
"message": "Your filters contain a field 'status' that doesn't appear on your model definition nor it's relations"
}
我稍后会根据需要为查询添加内容,但即使遵循文档,也会发生此错误,如果我没有覆盖默认 Controller ,API 工作正常。
提前感谢您的任何建议和建议。
编辑: models/post.settings.json
{
"kind":"collectionType",
"collectionName":"posts",
"info":{
"name":"Post",
"description":""
},
"options":{
"increments":true,
"timestamps":true,
"draftAndPublish":true
},
"attributes":{
"title":{
"type":"string",
"required":true
},
"images":{
"collection":"file",
"via":"related",
"allowedTypes":[
"images"
],
"plugin":"upload",
"required":false
},
"publishedAt":{
"type":"datetime"
},
"expiresAt":{
"type":"datetime"
},
"content":{
"type":"richtext"
},
"link":{
"type":"string"
},
"categories":{
"collection":"category",
"via":"posts"
},
"slug":{
"type":"uid",
"targetField":"title"
},
"originalPrice":{
"type":"decimal"
},
"salePrice":{
"type":"decimal"
},
"thumb":{
"model":"file",
"via":"related",
"allowedTypes":[
"images"
],
"plugin":"upload",
"required":false
}
}
}

最佳答案

正如错误消息所说,您没有 status模型中的字段。添加 enumeration字段 status值为 draft , published , 和 archived到您提到的模型 here .

关于javascript - "Your filters contain a field ' 状态 ' that doesn' t 出现在您的模型定义中,也不出现 's relations"(Strapi),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64561698/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com