所以我已经创建了一个控制器来创建用户配置文件,一切正常,它在数据库中创建了它,但是响应上却什么也没得到……起初我以为是我的路线,但经过一些测试通过在Controller中的方法的早期添加测试响应,它会返回正常。我不确定我缺少什么,就像我说的那样,它进入了数据库的罚款。
这是我的Controller.js
module.exports = function(req, res) {
var mod = {
createMerchantUser(req, res) {
var merchantDescription = 'Merchant for ' + req.body.email;
////////// If I add a test Response HERE it returns perfectly
///////// return{ message: "Testing response here..." }
// ANYTHING BELOW GETS CREATED IN DATABASE BUT DOESN'T PROVIDE A RESPONSE
var merchant = stripe.customers.create({
email: req.body.email,
description: merchantDescription
}, function(err, customer) {
var new_merchant_user = new MerchantUser({
email: req.body.email,
password: req.body.password,
accountConfirmed: false,
stripeID: customer.id
});
new_merchant_user.save(function(err, merchantUser) {
if (err) {
if (err.name === 'MongoError' && err.code === 11000) {
// Duplicate username
return res.status(500).send({ success: false, message: 'An account with this email already exists!' });
}
}
res.json({
data: {
merchantUser
}
});
});
});
}
}
return mod;
};
这是我的Route.js
app.post('/api/createMerchUser', (req, res) => {
return res.json(merchantUsersController.createMerchantUser(req, res))
})
您没有将数据返回到控制器中的路由。使用return
键。
return res.json({
data: {
merchantUser
}
});
尝试添加else块
if (err) {
if (err.name === 'MongoError' && err.code === 11000) {
// Duplicate username
return res.status(500).send({ success: false, message: 'An account with this email already exists!' });
}
} else {
return res.json({
data: {
merchantUser
}
}
尝试以下代码:
return res.status(200).type('application/json').send({
"statusCode": 200,
"statusMsg": "success,
"data": merchantUser
});
您是否收到304状态代码?
如果是,则
A 304 Not Modified响应代码指示所请求的资源自上次传输以来尚未修改。这通常表示无需将请求的资源重新传输到客户端,可以使用缓存版本。
尝试在获取请求时更改数据,然后查看是否获得响应数据。