簡體   English   中英

如何使用json解決無效值的問題

[英]How to solve the issue with invalid value with json

我正在嘗試從json接收一些數據,我正在對數據使用sequelize,問題是當我發送發布請求時,控制台會向我顯示錯誤:

Error: Invalid value { username: 'phernandez' }
    at Object.escape (C:\Users\pablo\Desktop\buildingapp\backend\node_modules\sequelize\lib\sql-string.js:65:11)
    at PostgresQueryGenerator.escape (C:\Users\pablo\Desktop\buildingapp\backend\node_modules\sequelize\lib\dialects\abstract\query-generator.js:963:22)
    at PostgresQueryGenerator._whereParseSingleValueObject (C:\Users\pablo\Desktop\buildingapp\backend\node_modules\sequelize\lib\dialects\abstract\query-generator.js:2420:41)

我已經在郵遞員發送中更改了不同的參數,以使用不同的類型,因為_我以為我發送的郵寄請求不正確,是由郵遞員使用x-form-www-urlencoded發送,然后再次與正文一起發送

 //this is the file app.js
   app.use(bodyParser.urlencoded({extended: false}));
   app.use(bodyParser.json());

//and the routes 

router.post('/',(req,res)=> {
    var usuario=req.body;
    console.log(usuario);
    Usuario.findOne({
        where: {username:usuario},
        attributes:['username','password','niv_adm']
    })
    .then(usuario => {
        console.log(usuario);
        res.sendStatus(200);
    })
    .catch(err => console.log(err))

});

我的期望是得到這個好,然后在我正在使用的數據庫中進行查詢並返回我需要的數據

仍然可以這樣簡化(因為使用兩種語言不會使任何事情變得容易):

router.post('/',(req,res)=> {
    Usuario.findOne({
        where: {username: req.body.username},
        attributes:['username','password','niv_adm']
    })
    .then(user => {
        console.log(user);
        res.sendStatus(200);
    })
    .catch(err => console.log(err))
});

我已經解決了這個問題,我只需要在模型部分中傳遞您正在使用的模型的參數,就像這樣,非常感謝您的幫助

 var usuario=req.body.username;

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM