节点中的Post方法呈现500错误

时间:2016-09-20 08:51:15

标签: node.js postman endpoints

我在节点中有几个post方法。其中一个工作,现在我在另一个工作。我已经尝试在res.body中切换键值对的顺序,重新启动我的后端服务器,以及其他一些东西,现在我没有想法。我正在使用邮递员,所以也许是一个问题,但也许你们会看到一个明显的错误,我没有。

以下是模型:

var UserGameSchema = new mongoose.Schema({
user: { type: String, required: true },
game: { type: String, required: true },
own: { type: Boolean, required: true }
});

var UserGame = mongoose.model('UserGame', UserGameSchema);

module.exports.UserGame = UserGame;

这是我的方法:

app.post('/mygames', function(req, res) {
UserGame.create({
    user: req.body.user,
    game: req.body.game,
    own: req.body.own
}, function(err, item) {
    if (err) {
        return res.status(500).json({
            message: 'Internal Server Error'
        });
    }
    res.status(201).json(item);


   });
});

这是我使用Postman发送的res.body

{
"user": "Colin",
"own": true,
"game": "Grand Theft Auto 5 (PS4)"
}

是的,我很确定我使用了正确的网址:http://localhost:8080/mygames

仍然出现这500错误。特别是邮递员在控制台中返回:

  

TypeError:无法读取未定义

的属性“user”

我不确定为什么用户会被定义。

感谢您的反馈。

如果寻找太多信息,这里是github repo: https://github.com/auldsy-ababua/gameswap

1 个答案:

答案 0 :(得分:0)

您仅对one specific route使用 if ($product_id) { $storeId = $objectManager->get('Magento\Store\Model\StoreManagerInterface')->getStore()->getId(); try { if(isset($params['super_attribute'][$product_id])){ $params_post = array(); $params_post['form_key'] = $this->formKey->getFormKey(); $params_post['qty'] = 1; $params_post['super_attribute'] = $params['super_attribute'][$product_id]; $finalproduct = $this->productRepository->getById($product_id, false, $storeId); $this->cart->addProduct($finalproduct, $params_post); } else { $params_post = array(); $params_post['form_key'] = $this->formKey->getFormKey(); $params_post['qty'] = 1; $finalproduct = $this->productRepository->getById($product_id, false, $storeId); $this->cart->addProduct($finalproduct, $params_post); } } catch (NoSuchEntityException $e) { } }

body-parser

所以它没有用于app.post('/users', jsonParser, function(req, res) { ... }); 路线。也可以在那里添加它,或者全局添加它:

POST /mygames