我希望你一切都好。
我有一个问题,我不知道如何解决它。我有一个带有食谱的 API,并且我设法在前端显示它们,但现在我无法使发布路由工作以在我的数据库中成功插入新食谱。如果有人能告诉我该怎么做,我将不胜感激。
节点:
app.post("/recipes", (req, res) => {
recipe = new Recipe({
_id: req.body._id,
title: req.body.title,
image: req.body.image,
description: req.body.description,
calories: req.body.calories,
cookingTime: req.body.cookingTime,
});
recipe.save(() => {
res.json(recipe);
});
});
打字稿服务添加配方
// Add recipe
post(uri: string, payload: Object) {
return this.http.post(`${this.url}/${uri}`, payload);
}
打字稿添加配方
ngOnInit(): void {
this.addRecipeForm = this.formBuilder.group({
_id: new FormControl(''),
title: new FormControl(''),
image: new FormControl(''),
description: new FormControl(''),
calories: new FormControl(''),
cookingTime: new FormControl(''),
});
}
export class RecipeModel {
_id!: String;
title!: String;
image!: String;
description!: String;
calories!: String;
cookingTime!: String;
}
显示食谱的食谱 HTML
<div *ngFor="let recipe of data">
<p>
{{ recipe.title }}
</p>
<img src="{{ recipe.image }}" alt="" />
<p>{{ recipe.description }}</p>
<p>{{ recipe.calories }}</p>
<p>{{ recipe.cookingTime }}</p>
<p>{{ recipe._id }}</p>
<button routerLink="/add">Add a new recipe here</button>
</div>
我现在要做的是按下按钮添加新配方并插入值,然后在我的数据库和 UI 中查看配方,我该如何管理它,非常感谢您的时间。
问候