nuxt.js - Nuxt.js - ssr,错误在将标头发送到客户端后无法设置标头
问题描述
我在 nuxt.js-ssr 环境中遇到了这个问题。
ERROR Cannot set headers after they are sent to the client
at ServerResponse.setHeader (_http_outgoing.js:535:11)
at p (node_modules/cookie-universal/dist/cookie-universal-common.js:1:1399)
at Object.set (node_modules/cookie-universal/dist/cookie-universal-common.js:1:1836)
at pages/index.js:145:21
at processTicksAndRejections (internal/process/task_queues.js:97:5)
原因?当 nuxt fetch(SSR) 调用 API 并写入 cookie 信息作为响应时发生
protected async fetch() {
axios.get('https://api.onstove.com/test/test')
.then((value: AxiosResponse<Test>) => {
console.log('data ::', value.data);
})
.then(() => {
console.log('cookie before ===> ', this.$cookies.get('T1'));
this.$cookies.set('T1', 'test122sdfdsfsfds34342');
console.log('cookie after ===> ', this.$cookies.get('T1'));
});
}
让我知道是否有人解决了它。
解决方案
如果您返回承诺,nuxt 将在向客户端发送数据之前等待它被解析。
protected async fetch() {
return axios.get('https://api.onstove.com/test/test')
.then((value: AxiosResponse<Test>) => {
console.log('data ::', value.data);
})
.then(() => {
console.log('cookie before ===> ', this.$cookies.get('T1'));
this.$cookies.set('T1', 'test122sdfdsfsfds34342');
console.log('cookie after ===> ', this.$cookies.get('T1'));
});
}
推荐阅读
- google-photos - Google Photos REST API“pageSize”和“pageToken”参数导致 400 错误请求
- apache-kafka - 使用 Kafka 协议将 Camel 与 EventHubs 连接
- python - 如何将json数据从单键转换为csv
- sql-server - SQL Server 中是否有 IF/AND,就像 Excel 中一样
- php - 使用 laravel auth 在 PWA 中离线时保持用户登录
- spring - Spring中路径变量的javax正则表达式验证
- mysql - Qt使用数据库检查登录
- javascript - 正则表达式从文件中选择 CSS 类名称
- c++ - 我可以在不包括的情况下使用 C 套接字吗
? - python - IndexError:列表索引超出“if...else...”中的范围