【问题标题】:POST 400 bad request when clicking fast快速点击时 POST 400 bad request
【发布时间】:2020-03-15 00:46:48
【问题描述】:

我目前正在使用 React、PostgreSQL 和 node.js 创建一个全栈 Web 应用程序

我能够检索 node.js 数据。但是,我需要等待至少 10 秒才能从我的数据库中检索另一个数据。当我尝试在 10 秒之前检索数据时,我得到一个 OST http://localhost:3001/api/search400(错误请求)。这很奇怪,因为我没有提取或插入任何大数据,只有一行。

感谢大家的帮助。

server.js

let pool = new pg.Pool({
    port: 5432,
    database: 'boost',
    max: 10,
    options: {
        encrypt: true
    }
});

var app = express();

app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: true }));


app.use(function(req, res, next) {
    res.header("Access-Control-Allow-Origin", "http://localhost:3000"); // update to match the domain you will make the request from
    res.header("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept");
    next();
});

pp.post('/api/search', function(request, response){
    var search = request.body.search_info;
    var search_values = ["%"+search+"%"];

    pool.connect((err,db,done) => {
        if(err){
            return response.status(400).send(err);
        }
        else{
            db.query("SELECT * FROM Users WHERE username LIKE $1", [...search_values], (err, table) => {
                done();
                if(err){
                    return response.status(400).send(err);
                }
                else{
                    db.end();
                    var result = [];
                    for(var i in table.rows){
                        result.push(table.rows[i].username);
                    }
                    response.status(201).send({value: result});
                }
            })
        }
    })
})

反应面

checkSearchResult(event){
        event.preventDefault();
        let data = {
            search_info: this.refs.searchInput.value,
        }
        var request = new Request('http://localhost:3001/api/search', {
            method: 'POST',
            headers: new Headers({ 'Content-Type' : 'application/json' }),
            body: JSON.stringify(data)
        });
        fetch(request).then((response) => {
            response.json().then((data) => {
                console.log(data);
            });
        }).catch(function(err){
            console.log(err);
        })
    }

网络标题

Request URL: http://localhost:3001/api/search
Request Method: POST
Status Code: 400 Bad Request
Remote Address: [::1]:3001
Referrer Policy: no-referrer-when-downgrade
Access-Control-Allow-Headers: Origin, X-Requested-With, Content-Type, Accept
Access-Control-Allow-Origin: http://localhost:3000
Connection: keep-alive
Content-Length: 2
Content-Type: application/json; charset=utf-8
Date: Mon, 09 Mar 2020 02:58:22 GMT
ETag: W/"2-vyGp6PvFo4RvsFtPoIWeCReyIC8"
X-Powered-By: Express
Accept: */*
Accept-Encoding: gzip, deflate, br
Accept-Language: en-US,en;q=0.9
Connection: keep-alive
Content-Length: 22
content-type: application/json
Host: localhost:3001
Origin: http://localhost:3000
Referer: http://localhost:3000/profile
Sec-Fetch-Dest: empty
Sec-Fetch-Mode: cors
Sec-Fetch-Site: same-site
User-Agent: Mozilla/5.0 (Linux; Android 6.0; Nexus 5 Build/MRA58N) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/80.0.3987.132 Mobile Safari/537.36
{search_info: "test"}
search_info: "test"

网络发起者

    checkSearchResult   @   TopNavigator.js:76
handleKeyPress  @   TopNavigator.js:45
callCallback    @   react-dom.development.js:188
invokeGuardedCallbackDev    @   react-dom.development.js:237
invokeGuardedCallback   @   react-dom.development.js:292
invokeGuardedCallbackAndCatchFirstError @   react-dom.development.js:306
executeDispatch @   react-dom.development.js:389
executeDispatchesInOrder    @   react-dom.development.js:414
executeDispatchesAndRelease @   react-dom.development.js:3278
executeDispatchesAndReleaseTopLevel @   react-dom.development.js:3287
forEachAccumulated  @   react-dom.development.js:3259
runEventsInBatch    @   react-dom.development.js:3304
runExtractedPluginEventsInBatch @   react-dom.development.js:3514
handleTopLevel  @   react-dom.development.js:3558
batchedEventUpdates$1   @   react-dom.development.js:21902
batchedEventUpdates @   react-dom.development.js:1060
dispatchEventForLegacyPluginEventSystem @   react-dom.development.js:3568
attemptToDispatchEvent  @   react-dom.development.js:4267
dispatchEvent   @   react-dom.development.js:4189
unstable_runWithPriority    @   scheduler.development.js:653
runWithPriority$1   @   react-dom.development.js:11061
discreteUpdates$1   @   react-dom.development.js:21918
discreteUpdates @   react-dom.development.js:1071
dispatchDiscreteEvent   @   react-dom.development.js:4168

【问题讨论】:

  • 您能发布您在 400 中遇到的错误吗?您可以在网络选项卡中查看它。
  • 添加了网络标头,抱歉不是 5 秒,更像是 10 秒
  • 响应正文在哪里?这是最重要的一点
  • @AritraChakraborty 响应正文是“{}”

标签: node.js reactjs postgresql


【解决方案1】:

我通过删除 db.end() 解决了这个问题。有谁知道为什么会导致这个问题?

【讨论】:

  • @keikai 哦,我很抱歉。我以为你想让我删除帖子并重新创建一个关于它的新帖子。我的道歉
【解决方案2】:

尝试连接数据库时出现错误 400

            return response.status(400).send(err);

你可以在它之前添加以下代码吗?

console.log("ERROR " , err);
return response.status(400).send(err);

并发布 console.log 信息?

对于您的 pg.Pool 配置,您可以尝试增加 max 吗?

let pool = new pg.Pool({
    port: 5432,
    database: 'boost',
    max: 10,
    options: {
        encrypt: true
    }
});

尝试设置max: 20

【讨论】:

  • 这不是答案。它应该在 cmets 中。没有日志你不知道。
  • 抱歉,我无法评论这个问题,因为需要 50 声望才能评论
  • 当我在 console.log 中添加时,我得到 POST localhost:3001/api/search 400 (Bad Request)。最多 20 个也没有帮助
  • 实际上,我在 processTicksAndRejections (内部/进程/task_queues.js:79:11)"
猜你喜欢
  • 1970-01-01
  • 2020-06-26
  • 1970-01-01
  • 1970-01-01
  • 2019-10-22
  • 1970-01-01
  • 2017-11-30
  • 2023-03-21
  • 1970-01-01
相关资源
最近更新 更多