【问题标题】:I'm getting a 200 status response from API Server, but the account still exists我从 API Server 收到 200 状态响应,但该帐户仍然存在
【发布时间】: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 状态响应,但帐户没有被删除?

这是我尝试在远程服务器上运行 npm start 时的屏幕截图。

【问题讨论】:

  • 既然你已经说过要建模destroy by id那么为什么不直接这样称呼它呢? Account.destroyById(req.accessToken.userId)
  • 我确实尝试过,但后来我查看了自定义方法中的“接受”,它期待一个对象..所以我给了它一个对象。

标签: javascript node.js loopbackjs strongloop


【解决方案1】:

你有很多方法:

1) 在实例上调用销毁:

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}});
    })
    ...

【讨论】:

  • 感谢您的回复。我已经尝试了您发布的两种方式,并且该帐户仍然存在于数据库中。是否有任何我可能忘记的其他信息可以解释为什么 destroy() 不起作用?
  • @VK1 你可以做console.log(req.accessTolen) 并把它的内容放在你的问题上吗?我怀疑req.accesssToken 不包含userId
  • 我在方法中放入了一个console.log,这样我就可以看到req.accessToken。但是,我不确定我如何/在哪里能够读取日志。根据环回文档,我只需要从类型“node”开始。但据我所知,它已经在远程服务器上运行。我现在很迷茫。
  • 我尝试在调试模式下启动服务器:export DEBUG=*; npm start 但这给了我错误:错误:听 EADDRINUSE 0.0.0.0:3000
  • @VK1 1. 使用任务管理器或终端终止该进程。 2.在Account.findById上方添加console.log(req.accessToken); process.exit(0);,然后重试。不需要DEBUG 只需运行:npm start
猜你喜欢
  • 2020-05-05
  • 1970-01-01
  • 2020-03-12
  • 2021-10-01
  • 1970-01-01
  • 1970-01-01
  • 2015-09-30
  • 1970-01-01
  • 2018-11-26
相关资源
最近更新 更多