【发布时间】:2019-01-11 16:52:05
【问题描述】:
这是我当前的服务器堆栈:
-服务器操作系统
Ubuntu 16.04 -网络服务器
NGINX -服务器应用程序
Loopback.js 和 PM2 -数据库
MariaDB
我在 Loopback 中编写了一个自定义方法来通过 ID 号删除帐户。当我使用 POSTMAN 对其进行测试时,我得到了 200 响应,但是当我手动检查 MariaDB(SELECT * from Account WHERE id=1111) 时,它显示 id/account 仍然存在。
这是我为loopback写的方法:
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 状态响应,但帐户没有被删除?
【问题讨论】:
-
既然你已经说过要建模
destroy by id那么为什么不直接这样称呼它呢?Account.destroyById(req.accessToken.userId) -
我确实尝试过,但后来我查看了自定义方法中的“接受”,它期待一个对象..所以我给了它一个对象。
标签: javascript node.js loopbackjs strongloop