sails-mysql错误处理:TypeError:将循环结构转换为JSON

时间:2018-02-19 13:56:31

标签: json node.js sails.js

服务器:Sailsjs

数据库:mysql

使用的节点库:sails-mysql(0.11.5)

我正在尝试从sailsjs节点服务器在mysql中插入数据。

要插入的数据:

{
    "first_name": "Admin Testing",
    "last_name": "User",
    "email": "admin14@testing.com",
    "role_id": 8
}

其中role_id与模型角色具有外键关系。

这里role_id:8在角色表中没有值。因此,插入时,它会抛出错误。但是,由于发生以下错误,我们无法通过控制台或json.stringify发生错误:

TypeError: Converting circular structure to JSON
    at JSON.stringify (<anonymous>)
    at /var/www/aitachatbot/server/api/controllers/UserController.js:38:42
    at wrapper (/var/www/aitachatbot/server/node_modules/waterline/node_modules/lodash/index.js:3602:19)
    at applyInOriginalCtx (/var/www/aitachatbot/server/node_modules/waterline/lib/waterline/utils/normalize.js:421:80)
    at wrappedCallback (/var/www/aitachatbot/server/node_modules/waterline/lib/waterline/utils/normalize.js:331:16)
    at error (/var/www/aitachatbot/server/node_modules/switchback/lib/normalize.js:42:31)
    at _switch (/var/www/aitachatbot/server/node_modules/switchback/lib/factory.js:56:28)
    at /var/www/aitachatbot/server/node_modules/sails-hook-validation/lib/create.js:66:21
    at /usr/local/lib/node_modules/sails/node_modules/waterline/lib/waterline/query/dql/create.js:223:14
    at wrapper (/usr/local/lib/node_modules/sails/node_modules/lodash/index.js:3592:19)
    at applyInOriginalCtx (/usr/local/lib/node_modules/sails/node_modules/waterline/lib/waterline/utils/normalize.js:421:80)
    at wrappedCallback (/usr/local/lib/node_modules/sails/node_modules/waterline/lib/waterline/utils/normalize.js:335:16)
    at error (/usr/local/lib/node_modules/sails/node_modules/waterline/node_modules/switchback/lib/normalize.js:42:31)
    at _switch (/usr/local/lib/node_modules/sails/node_modules/waterline/node_modules/switchback/lib/factory.js:56:28)
    at afterwards (/usr/local/lib/node_modules/sails/node_modules/waterline/lib/waterline/adapter/dql.js:87:16)
    at wrapper (/usr/local/lib/node_modules/sails/node_modules/lodash/index.js:3592:19)
    at applyInOriginalCtx (/usr/local/lib/node_modules/sails/node_modules/waterline/lib/waterline/utils/normalize.js:421:80)
    at wrappedCallback (/usr/local/lib/node_modules/sails/node_modules/waterline/lib/waterline/utils/normalize.js:335:16)
    at error (/usr/local/lib/node_modules/sails/node_modules/waterline/node_modules/switchback/lib/normalize.js:42:31)
    at _switch (/usr/local/lib/node_modules/sails/node_modules/waterline/node_modules/switchback/lib/factory.js:56:28)
    at sendBackError (/var/www/aitachatbot/server/node_modules/sails-mysql/lib/connections/spawn.js:97:11)
    at Object.poolfully [as releaseConnection] (/var/www/aitachatbot/server/node_modules/sails-mysql/lib/connections/release.js:28:12)
    at /var/www/aitachatbot/server/node_modules/sails-mysql/lib/connections/spawn.js:90:37
    at Query._callback (/var/www/aitachatbot/server/node_modules/sails-mysql/lib/adapter.js:395:27)
    at Query.Sequence.end (/var/www/aitachatbot/server/node_modules/sails-mysql/node_modules/mysql/lib/protocol/sequences/Sequence.js:96:24)
    at Query.ErrorPacket (/var/www/aitachatbot/server/node_modules/sails-mysql/node_modules/mysql/lib/protocol/sequences/Query.js:94:8)
    at Protocol._parsePacket (/var/www/aitachatbot/server/node_modules/sails-mysql/node_modules/mysql/lib/protocol/Protocol.js:280:23)
    at Parser.write (/var/www/aitachatbot/server/node_modules/sails-mysql/node_modules/mysql/lib/protocol/Parser.js:73:12)

我们如何在sailsjs中处理此错误?

1 个答案:

答案 0 :(得分:0)

理论上,你不希望这些错误出现在生产中......即使他们这样做了,错误对象的完整json转储可能不是普通用户想要的见。

您可能还在询问如何在调试时从此错误中获取完整信息。我想那里有个好消息,你似乎已经知道错误的根本原因了!

我们在节点中的错误对象可以根据它们的来源具有完全不同的结构,但是我还没有遇到没有将错误智能转换为字符串的错误。这意味着即使console.log(err)失败,类似:console.log('Got an error: ' + err)的内容也可能有效,将错误转换为字符串。

代码可能是这样的:

User.create({
    "first_name": "Admin Testing",
    "last_name": "User",
    "email": "admin14@testing.com",
    "role_id": 8
}).exec(function(err, createdUser) {
    if (err) {
        var readableErr = '' + err;
        console.log('Error creating user: ' + readableErr);
        // return something the client will recognize and interpret
        // you can use http error codes, or your own conventions
        return res.send({
            success: false,
            message: readableErr
        });
    }
    // do something with createdUser...
});