nodejs服务器获取请求不可靠



这是我的邮寄路线


router.post('/', async function (req, res) {
// console.log(req.user);
let ts = Date.now();
let date_ob = new Date(ts);
let date = date_ob.getDate();
let month = date_ob.getMonth() + 1;
let year = date_ob.getFullYear();
let time = date_ob.getUTCHours() + "-"+date_ob.getUTCMinutes()+"-"+date_ob.getUTCMilliseconds()
let createDate =  year + "-" + month + "-" + date+"T"+time;
// console.log(req.body.image);
let data = await Example.create({
authorName:req.user.username, 
createDate: createDate, 
description:req.body.desc,
title:req.body.title,
tags:req.body.tag,
examples: req.body.code,
image: req.body.image
}).then(function(data){
res.status(200).send(data);
}).catch(function(err){
res.status(500).send(err);
});
});

单击保存示例按钮时,客户端发送一个获取请求以将示例保存到我的数据库

async function saveExample(data) {
// call snapshot function
let snapshot = getCanvasSnapshot();
console.log(snapshot);

const url = "/examples";
var tag = "";
if (document.getElementById("easy").checked) {
tag = "easy";
} else if (document.getElementById("medium").checked) {
tag = "medium";
} else {
tag = "tough";
}
let jsonData = {
code: data,
desc: document.querySelector("#desc1").value,
title: document.querySelector("#title1").value,
tag: tag,
image: snapshot,
};
// console.log("Json Data: ", jsonData);
//
fetch(url, {
method: "POST",
mode: "cors",
cache: "no-cache",
credentials: "same-origin",
headers: {
"Content-Type": "application/json",
},
body: JSON.stringify(jsonData), // body data type must match "Content-Type" header
})
.catch((err) => {
console.log("ouput");
console.log(err);
});
}

该快照包含一个带有meta标签的url编码base64格式的画布截图。

当我第一次尝试保存一个示例时,它没有任何错误,但是当我第二次尝试这样做时,它抛出这个错误。

2022-05-12T19:11:02.796496+00:00 app[web.1]: BadRequestError: request aborted
2022-05-12T19:11:02.796497+00:00 app[web.1]: at IncomingMessage.onAborted (/app/node_modules/raw-body/index.js:238:10)
2022-05-12T19:11:02.796498+00:00 app[web.1]: at IncomingMessage.emit (node:events:390:28)
2022-05-12T19:11:02.796498+00:00 app[web.1]: at IncomingMessage._destroy (node:_http_incoming:179:10)
2022-05-12T19:11:02.796498+00:00 app[web.1]: at _destroy (node:internal/streams/destroy:102:25)
2022-05-12T19:11:02.796499+00:00 app[web.1]: at IncomingMessage.destroy (node:internal/streams/destroy:64:5)
2022-05-12T19:11:02.796499+00:00 app[web.1]: at abortIncoming (node:_http_server:596:9)
2022-05-12T19:11:02.796499+00:00 app[web.1]: at socketOnClose (node:_http_server:590:3)
2022-05-12T19:11:02.796499+00:00 app[web.1]: at Socket.emit (node:events:402:35)
2022-05-12T19:11:02.796500+00:00 app[web.1]: at TCP.<anonymous> (node:net:687:12)

关于我的服务器如何配置的更多信息

app.use(cors());
app.use(bodyParser.json({limit: '50mb'}));
app.use(bodyParser.urlencoded({limit:'50mb',extended:false}));

我对可能导致这种情况的原因感到茫然,它有时会将示例保存到我的dv中,有时则不会。

1这不是必需的。

let ts = Date.now();
let date_ob = new Date(ts);

只使用:

let date_ob = new Date();

还可以将async/await与。then/混合使用。抓住所有的时间;另外,你没有。then来获取数据(只有。catch);

最重要的是- fetch是为url/example和您的路径只定义为/

如果我们这边没有更多的代码或更多的测试,我无法帮助你更多

相关内容

  • 没有找到相关文章

最新更新