[Support] 使用sse返回的第一条数据会出现[object Object] #4174
-
Describe the problem(描述问题)使用sse返回数据,但是返回的数据中意外地出现了[object Object],这影响到了我的解析。虽然可以加以处理,但是会引起我的不安。 import {
Inject,
Controller,
Get,
Query,
HttpServerResponse,
} from '@midwayjs/core';
import { Context } from '@midwayjs/koa';
import { UserService } from '../service/user.service';
@Controller('/api')
export class APIController {
@Inject()
ctx: Context;
@Inject()
userService: UserService;
@Get('/get_user')
async getUser(@Query('uid') uid) {
const res = new HttpServerResponse(this.ctx).sse();
setTimeout(() => {
res.send({
data: 'test',
});
res.end();
}, 3000);
return res;
}
} Midway Versions(Midway 版本)
✓ @midwayjs/faas-typings(not installed)
|
Beta Was this translation helpful? Give feedback.
Replies: 3 comments 9 replies
-
const res = new HttpServerResponse(this.ctx).sse();
setTimeout(() => {
res.send({
data: 'test',
});
res.end();
}, 3000);
return res; 代码返回的 res 确是是个对象啊。setTimeout 应该改成 Promise 并返回这个 Promise |
Beta Was this translation helpful? Give feedback.
-
try: return new Promise((resolve) => {
setTimeout(() => {
res.send({
data: 'test',
});
res.end();
resolve(res); // <===
}, 3000);
}); |
Beta Was this translation helpful? Give feedback.
-
请使用 EventSource 来接收数据,如果想打印在页面上,使用 |
Beta Was this translation helpful? Give feedback.
大概知道原因了,在第一次数据传输时,发送了一个{data: ' :ok'} 对象,这个逻辑参考了社区库的实现,有点问题,正确的应该拿到
才对