如何解决SQL查询中序列化BigInt的警告



我在进行API调用时继续收到警告,其内容为:

TypeError: Do not know how to serialize a BigInt at JSON.stringify(<anonymous>)

这并不能阻止我查询数据库,但我不希望每次API调用都向我发出警告。

我使用的是uuidv4(此处为npm包(,它返回36个字符(包括连字符(的随机字符串。我的API调用如下:

router.post('/:plantName/:quantity/:description/:addedBy', (req, res, next) => {
console.log('API request to add a new plant');
const newPlant = {
name: req.params.plantName,
quantity: req.params.quantity,
description: req.params.description,
addedBy: req.params.addedBy
}
db.addNewPlant(newPlant, (error, results) => {
if(error) {
res.status(500).send('Server Error');
return;
}
res.send(results);
})
})

并从CCD_ 3导出为CCD_。。。

// Add a new plant
const addNewPlant = async (plant, callback) => {
const newID = uuidv4();
const query = `INSERT INTO plants VALUES ("${newID}", "${plant.name}", 
${plant.quantity}, "${plant.description}", "${plant.addedBy}")`;
try {
const connection = await pool.getConnection();
const response = await connection.query(query);
callback(false, response);
} catch (error) {
console.error(error);
callback(true);
return;
}
}

问题不在于SQL查询,而在于我从中间件返回的内容。在第一块代码的末尾,res.send(results)没有发送我需要或想要的信息。我用res.status(200).send('Plant successfully added')替换了它,它按预期工作。

最新更新