我一直在尝试使用 Typegoose 使用类转换器库来完成 Mongodb 序列化部分的 NestJs 示例。https://docs.nestjs.com/techniques/serialization中给出的示例仅展示了如何在 TypeORM 中使用序列化。我对 Typegoose 遵循了相同的流程。这是我到目前为止所尝试过的。
// cat.domain.ts
import { prop } from '@typegoose/typegoose';
export class Cat {
@prop()
name: string;
@prop()
age: number;
@prop()
breed: string;
}
// cats.service.ts
@Injectable()
export class CatsService {
constructor(
@InjectModel(Cat) private readonly catModel: ReturnModelType<typeof Cat>,
) {}
findAll(): Observable<Cat[]> {
return from(this.catModel.find().exec());
}
findOne(id: string): Observable<Cat> {
return from(this.catModel.findById(id).exec());
}
...
}
// cat.response.ts
import { ObjectId } from 'mongodb';
import { Exclude, Transform } from …Run Code Online (Sandbox Code Playgroud) 如果出现未处理的错误,SpringBoot 应用程序的默认服务器响应是
{
"timestamp": 1594232435849,
"path": "/my/path",
"status": 500,
"error": "Internal Server Error",
"message": "this request is failed because of ...",
"requestId": "ba5058f3-4"
}
Run Code Online (Sandbox Code Playgroud)
我想在应用程序路由的 Springdoc 注释中描述它。
假设有一个标准类DefaultErrorResponse(只是一个模拟名称),它可能如下所示:
@Operation(
// ... other details
responses = {
// ... other possible responses
@ApiResponse(
responseCode = "500",
content = @Content(schema = @Schema(implementation = DefaultErrorResponse.class)))
}
)
Run Code Online (Sandbox Code Playgroud)
在更糟糕的情况下,这样的类不存在,Spring 仅使用Map底层来创建响应。那么这个注释将更加详细,包括明确提及响应中包含的每个字段。
显然,对于大多数路线来说,这部分@ApiResponse(responseCode="500",...是相同的,并且最好减少重复。
在文档中引入默认错误响应描述的正确方法是什么?
我尝试使用 ts-jest 模拟获取响应,但遇到打字稿错误
import { mocked } from 'ts-jest/utils';
import fetch from 'node-fetch';
import { getInstallationAccessToken } from './index';
const mockedFetch = mocked(fetch, true);
describe('getInstallationAccessToken', () => {
const TODAY = new Date();
const TOMORROW = new Date();
TOMORROW.setDate(TODAY.getDate() + 1);
beforeEach(() => {
mockedFetch.mockResolvedValue({
status: 200,
json: async () => ({
token: 'MOCKED_GITHUB_INSTALLATION_ACCESS_TOKEN',
expires_at: TOMORROW.toISOString()
})
});
jest.clearAllMocks();
});
test('generates github app jwt token', async () => {
await getInstallationAccessToken();
expect(mockedJwt.sign).toBeCalledTimes(1);
});
})
Run Code Online (Sandbox Code Playgroud)
在此示例中,我收到以下错误:
Argument of type '{ status: …Run Code Online (Sandbox Code Playgroud) Spring 5 引入了 ResponseStatusException,这让我陷入了困境,不知道在什么情况下可以使用 ResponseStatusException 和 ControllerAdvice,因为它们非常相似。
谁能帮我这个?
error-handling exception response httpresponse controller-advice
使用此函数可以展平从版本 4 上的 Strapi 返回的响应。帮助您摆脱数据和属性属性
这将为您提供与 Strapi 版本 3 相同的响应结构。这将帮助您轻松从版本 3 迁移到版本 4。
如何使用它?
注意:此处的数据是从 Strapi 版本 4 返回的响应。
export const flattenObj = (data) => {
const isObject = (data) =>
Object.prototype.toString.call(data) === "[object Object]";
const isArray = (data) =>
Object.prototype.toString.call(data) === "[object Array]";
const flatten = (data) => {
if (!data.attributes) return data;
return {
id: data.id,
...data.attributes,
};
};
if (isArray(data)) {
return data.map((item) => flattenObj(item));
}
if (isObject(data)) {
if (isArray(data.data)) { …Run Code Online (Sandbox Code Playgroud) 我想将我的json响应存储在一个全局变量中,所以,我可以通过我的应用程序使用它,而不必多次发出getJSON请求.
var data;
$.getJSON("panorama.json",function(json){
data = json.images[0].src;
console.log(data);
});
console.log(data);
Run Code Online (Sandbox Code Playgroud)
如果我在实际请求中记录它很好,但我在其他地方得到"未定义".评论任何评论.
编辑[从评论中复制]:试过......
$.myglobals = { result: "unset" }
$(document).ready(function() {
$.getJSON( "panorama.json", function(json) {
$.myglobals.result = json.images[0].src;
console.log($.myglobals.result);
});
console.log($.myglobals.result);
}) ;
Run Code Online (Sandbox Code Playgroud)
第一个日志没问题,第二个日志未定义.
编辑[从答案中复制]:
实际上两种方法都有效
有趣的是,我的请求是在一个函数中,我试图在同一函数中的请求后立即访问我的全局变量
当我在功能之外加入它时,它就像一个魅力
我想创建一个获取json响应的http get请求.在该会话响应中,我想将值存储到我的会话中.这怎么可以实现?
谢谢
我需要知道是否Response.Clear()只是清除内容还是删除标题?换句话说,如果我使用Response.Clear(),我是否还需要使用Response.ClearHeader()?
我在symfony2中有一个404处理程序,它是一个EventListener.
对于某些404,我进行重定向,效果很好.对于浏览器,不会抛出404.
new RedirectResponse( $newURL );
Run Code Online (Sandbox Code Playgroud)
该行基本上用200替换404状态代码.
但在其他情况下,我想返回一些内容,而不是404消息,而是一些替换数据.像这样的东西:
$response = new Response( $returnJSONMessage );
$response->headers->set( 'Content-Type', 'application/json' );
$response->setStatusCode(200);
Run Code Online (Sandbox Code Playgroud)
代码方面,这很好,但它不会阻止返回404.我猜是因为它在这个范围内:
$event->setResponse( $theResponse );
Run Code Online (Sandbox Code Playgroud)
其类型为GetResponseForExceptionEvent.
我需要调用什么来让它以200而不是404的形式返回我的数据.就像RedirectResponse似乎一样.我试过了:
$event->stopPropagation();
Run Code Online (Sandbox Code Playgroud)
但它有点事后.似乎$ event-> setResponse中的任何内容都不是RedirectResponse,在这种情况下标记为404.
有任何想法吗?
我正在尝试使用window.fetch()从服务器获取json,但无法从响应中获取数据.我有这个代码:
> let url =
> 'https://api.flightstats.com/flex/schedules/rest/v1/json/from/SYD/to/MEL/departing/2016/3/28?appId=f4b1b6c9&appKey=59bd8f0274f2ae88aebd2c1db7794f7f';
> let request = new Request (url, {
> method: 'GET',
> mode: 'no-cors'
> });
>
> fetch(request)
> .then(function(response){
> console.log(response)
> });
Run Code Online (Sandbox Code Playgroud)
看来这个请求是成功的,我在网络选项卡中看到状态200和响应体json - 状态和响应.但在console.log我看不到json对象 - 控制台日志图像
我不明白为什么我在console.log中看不到json