javascript - NgOnInit 没有在服务订阅中执行代码
问题描述
这是我的NgOnInit
方法。在控制台中只执行“hello before”。我试过做.subscribe( () => console.log("hello after"))
,但里面没有任何工作。
ngOnInit(): void {
console.log("hello before")
this.checkoutService.getTemplates().subscribe(({ templates }) => {
console.log("hello after")
this.templates = templates;
console.log(templates)
})
}
结账服务:
getTemplates(): Observable<{templates: Template[]}> {
return this.http.get<{templates: Template[] }>("http://localhost:3000/checkout")
}
/结帐端点处理程序:
router.get("", (req, res, next) => {
var templates = [];
var realTemps = [];
const token = req.headers.authorization.split(" ");
const decoded = jwt.verify(token[1], 'secretkey');
decodedFactoryId = decoded.factoryId
Template.findAllByFactoryId(decodedFactoryId)
.then((results) => {
for (var i = 0; i < results.length; i++) {
// THIS IS AN AVOIDABLE LOOP, quadratic time must be prevented by correctly formatting the data (TODO)
for (var j = 0; j < results.length; j++) {
if (results[i][j].id) {
templates.push(results[i][j].id)
}
}
}
}).then(() => {
// Promise.all
return Promise.all(templates.map(template => Template.findByTemplateId(template).then(result => {
console.log("here is the result: " + JSON.stringify(result[0]))
realTemps.push(result);
})));
})
.then(() => {
return realTemps;
})
})
有 2 个请求发送到 http://localhost:3000/checkout。其中一个回应了200,另一个没有回馈任何东西。
这是我的网络标签:
如何checkoutservice
在NgOnInit
方法中获取代码以实际执行?
解决方案
推荐阅读
- c++ - 矢量 Push_Back VS Emplace_Back
- php - 管理员用户名和应用程序的用户名可以相同吗?
- c - 使用 AVX 的平铺矩阵乘法
- javascript - 触发调度时,Setstate 变得混乱
- ios - RefreshControl 在其他选项卡中以类似方式使用的代码的选项卡之一中不起作用
- java - 如何使用循环取消算法解决分配问题
- c# - 多个控件上的相同 MouseMove 事件
- html-table - 获取 Tabulator js 上的最后一行索引
- java - 从 InputStream 将 tif 文件编码为 Base64
- module - nextjs 组件库