gpt4 book ai didi

node.js - 将数据从 angular 发送到 Node api

转载 作者:行者123 更新时间:2023-12-03 22:43:39 25 4
gpt4 key购买 nike

我是初学者,所以如果我的问题不相关或不易于解决,我深表歉意,但我无法解决我的问题 4 天。

我正在开发一个 api,它将从 angular 形式接收数据,然后将其与 sequelize 一起存储在 mariadb 表上。

当我提交我的表单时,它将通过 url,通过 api 将其发布在表中,我可以看到 req.body 已定义,并且它到达了 api,但是我在我的 angular http 帖子中有错误被抛出(错误 500)。
对于 postman ,我看到我的 url 无法访问,我无法弄清楚我的错误是来自 post angular,还是来自我的模型的后端,即使错误 500 是针对后端的。

我试图重新定义我的 Sequelize 模型,解析和字符串化我发送的对象,尝试其他 url ......但没有任何效果。

所以这是我提交表单时调用的函数,函数 formatrequestschedulest 返回我要发布的对象。

onSubmitScheduled(){
let OndemandScheduledRequest = this.formatRequestScheduled(this.selectedHotel, this.selectedCheckInDate, this.selectedCheckOutDate, this.selectedNumber, this.selectedCurrency, this.selectedReportName, this.selectedUser, this.selectedEmail, this.selectedFormat);

console.log(OndemandScheduledRequest);
this.saveScheduledRequest(OndemandScheduledRequest);
return this.openDialog();
}


saveScheduledRequest(onDemandScheduledRequest: OnDemandScheduledRequest){
this.http.post('/api/ScheduledReport', JSON.stringify(onDemandScheduledRequest), {headers: {'Content-Type': 'application/json'}})
.subscribe(res=>{ console.log(res
)
console.log('ok')},
(err) =>
console.log('an error'))
/*console.log(err))*/
}

这是我的 api:
var request= require('../controller/ondemandscheduledrequests');
var router = express.Router();


router.get('/getreport',request.create);
router.post('/ScheduledReport',request.create);

我也可以提供我得到的错误,但我不知道我是否可以将图片加入问题中。

我只想将我的表单数据存储到我的表中,但我收到了错误 500。

this is the error i get

最佳答案

使用如下管道并尝试一次

  this.http.post('/ScheduledReport', JSON.stringify(onDemandScheduledRequest).pipe().subscribe(val => {
console.log(val, "service");
, {headers: {'Content-Type': 'application/json'}})}

关于node.js - 将数据从 angular 发送到 Node api,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55727556/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com