这是我当前的服务器堆栈:
-服务器操作系统
Ubuntu 16.04 -Web服务器
NGINX -服务器应用程序
Loopback.js和PM2 -数据库
MariaDB
我在回送中编写了一个自定义方法,以按ID号删除帐户。当我使用POSTMAN进行测试时,收到200响应,但是当我手动检查MariaDB(SELECT * from WHERE id = 1111的帐户)时,它表明ID /帐户仍然存在。
这是我为回送编写的方法:
Account.deleteThisAccount = function (req, callback) {
// check if req.accessToken exists
if (!req.accessToken) throw new Error('Access token not provided');
// check if req.accessToken has userId
if (!req.accessToken.userId) {
console.log('accessToken:', req.accessToken); // to see contents
throw new Error('Access token does not contain userId');
}
Account.findById(req.accessToken.userId)
.then((account) => {
if (!account) {
throw new Error('Cannot find user')
}
return Account.destroy({
where: {
id: req.accessToken.userId
}
})
})
.then(() => {
callback(null);
}).catch(error => {
callback(error);
})
}
Account.remoteMethod(
'deleteThisAccount', {
http: {
path: '/deleteThisAccount',
verb: 'del'
},
accepts: [
{ arg: 'req', type: 'object', http: { source: 'req' } }
],
returns: 'hopefully this works'
}
)
有任何想法为什么我会收到200个状态响应,但是该帐户没有被删除?
答案 0 :(得分:1)
您有很多方法:
1)在实例上调用destroy:
Account.findById(req.accessToken.userId)
.then((account) => {
if (!account) {
throw new Error('Cannot find user')
}
return account.destroy();
})
...
2)修复方法调用:
Account.findById(req.accessToken.userId)
.then((account) => {
if (!account) {
throw new Error('Cannot find user')
}
return Account.destroy({where: {id: req.accessToken.userId}});
})
...
如果没有帮助,请确保您正确使用req.accessToken.userId
:
// check if req.accessToken exists
if (!req.accessToken) throw new Error('Access token not provided');
// check if req.accessToken has userId
if (!req.accessToken.userId) {
console.log('accessToken:', req.accessToken); // to see contents
throw new Error('Access token does not contain userId');
}
Account.findById(req.accessToken.userId)
.then((account) => {
if (!account) {
throw new Error('Cannot find user')
}
return Account.destroy({where: {id: req.accessToken.userId}});
})
...