在Express中放路线不起作用 - sequelize / postgres

时间:2018-04-16 22:14:39

标签: express sequelize.js

我有一个应用程序的sequelize put路径,用于跟踪垃圾堆的位置(当人们将垃圾倾倒在路边时)。有问题的记录,Item,是单个站点的记录。记录没有关系。

我正试图运行这条路线。我希望如此,如果用户提供的数据不同于已记录的数据,则数据将被更新。但路线中途停了下来。我不知道为什么。这是路线:

// UPDATE ITEM
router.put('/update/:id', (req, res) => {
    let data; 
    Item 
        .find({ where: { id: req.params.id}})
        .then(data => {
            console.log(`Here is data: ${data.description}`)
            let newDescription = req.body.description ? req.body.description : data.description; 
            console.log("passed description")
            let newStreet = req.body.street ? req.body.street : data.street;
            console.log("passed street")
            let newCrossStreet = req.body.cross_street ? req.body.cross_street : data.cross_street; 
            console.log("passed cross")
            let newAddress = req.body.address ? req.body.address : data.address;
            console.log("passed address")
            let newCity = req.body.city ? req.body.city : data.city; 
            console.log("passed city")
            let newEmail = req.body.email ? req.body.email : data.email;
            console.log("passed email") 
            let newPhone = req.body.phone ? req.body.phone : data.phone; 
            console.log("passed phone")
            return Item 
                .update({ 
                    description: newDescription, 
                    street: newStreet, 
                    cross_street: newCrossStreet, 
                    address: newAddress, 
                    city: newCity, 
                    email: newEmail, 
                    phone: newPhone
                 }, { where: { id: req.params.id }})
                 .then(item => {
                     res.status(204).json({ item })
                 })
                 .catch(err => {
                     console.error(err)
                     res.status(500).json({ message: 'There was a problem' }); 
                 })
        })
        .catch(err => {
            res.status(400).json({ message: 'No record was found' }); 
        }); 
});

我在那里有console.logs来查看路由停止的位置。这是控制台输出:

App is listening on port 8080
Executing (default): SELECT "id", "description", "street", "cross_street", "address", "city", "photo_url", "email", "phone", "desired_reply", "completed", "date_reported", "date_completed" FROM "items" AS "Item" WHERE "Item"."id" = '3';
Here is data: Old clothes on the road
::1 - - [16/Apr/2018:22:09:38 +0000] "PUT /item/update/3 HTTP/1.1" 400 33

正如您所看到的,它只能访问第一个console.log。然后似乎停止了?我不知道为什么!任何人都可以帮我看看我在做错了吗?谢谢!

0 个答案:

没有答案