gpt4 book ai didi

javascript - POST 请求返回 [Object, object]

转载 作者:行者123 更新时间:2023-11-29 12:57:23 25 4
gpt4 key购买 nike

我正在尝试发送一个 POST 请求(使用 postman )以在我的 PostgreSQL 数据库中创建一个条目。但是,目前我的 POST 请求正在返回 [object, Object],我不知道为什么。

这是处理请求的代码。

//enable the router
app.use('/', router);

//Create
router.post('/api/v1/todos', (req, res, next) => {
const results = [];
console.log("Congrats you hit the create function!!");

//grab data from http-request, 'complete' is from a boolean value in the database
const data = {text: req.query.text, complete: false };

//give error if not recieving a request
if(!req.query.text) res.send(400, "The Request data is: " + req.query);

//get a prostgres client from the connection pool
pg.connect(connectionString, (err, client, done) => {
//handle connection errors
if (err) {
done();
console.log(err);
return res.status(500).json({success: false, data: err});
}

//SQL Query -> Insert Data
client.query('INSERT INTO items(text, complete) values($1,$2)',
[data.text, data.complete]);

//SQL Query -> Select Data
const query = client.query("SELECT * FROM items ORDER BY id ASC");

//Stream results back one row at a time
query.on('row', (row) => {
results.push(row);
});

//After all data is returned, close connection and return the results
query.on('end', () => {
done();
return res.json(results);
//return res.json(data);
});
});
});

这是我的帖子 POST image

为什么我的数据返回对象?我该怎么做才能解决这个问题?

Here is my full code .

最佳答案

您将一个字符串与一个对象连接起来,这会将对象转换为 [object Object] 形式的字符串。

这是你的问题:if(!req.query.text) res.send(400, "The Request data is: "+ req.query);

req.query 是一个对象(您只是检查它的文本属性是否存在)并且您正在将一个字符串连接到该对象。

关于javascript - POST 请求返回 [Object, object],我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39782022/

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