我对将数据插入MySQL DB有一些疑问。实际上,我已经做到了。但是我认为代码太长了,因此如果服务器或其他方面出现问题,它看起来很糟糕并且很难修改。
这是我的代码,代码真的很长,所以我想知道是否有一些解决方案可以使代码更短。
结果变量具有JSON数据,看来我可以在数据库查询中使用它。我已经认为我可以使用数组或对象,并且尝试了一些代码,但是它们不起作用。
router.post("/user", function(req, res, next) {
let id = req.body.id;
let githubAPI = "https://api.github.com/users/";
let options = {
url: githubAPI + id,
headers: {
"User-Agent": "request"
}
};
console.log(id);
request(options, function(error, response, data) {
if (error) {
throw error;
}
// result have JSON Data
let result = JSON.parse(data);
let nick = result.login;
let id = result.id;
let node_id = result.node_id;
let avatar_url = result.avatar_url;
let gravatar_id = result.gravatar_id;
let url = result.url;
let html_url = result.html_url;
let followers_url = result.followers_url;
let following_url = result.following_url;
let gists_url = result.gists_url;
let starred_url = result.starred_url;
let subscriptions_url = result.subscriptions_url;
let organizations_url = result.organizations_url;
let repos_url = result.repos_url;
let events_url = result.events_url;
let received_events_url = result.received_events_url;
let type = result.type;
let site_admin = result.site_admin;
let name = result.name;
let company = result.company;
let blog = result.blog;
let location = result.location;
let email = result.email;
let hireable = result.hireable;
let bio = result.bio;
let public_repos = result.public_repos;
let public_gists = result.public_gists;
let followers = result.followers;
let following = result.following;
let created_at = result.created_at;
let updated_at = result.updated_at;
if (bio == null) {
bio = "Developer";
}
db.query(
`INSERT INTO user (login, id, node_id, avatar_url, gravatar_id, url, html_url, followers_url, following_url, gists_url, starred_url, subscriptions_url, organizations_url, repos_url, events_url, received_events_url, type, site_admin, name, company, blog, location, email, hireable, bio, public_repos, public_gists, followers, following, created_at, updated_at) VALUES (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)`,
[
nick,
id,
node_id,
avatar_url,
gravatar_id,
url,
html_url,
followers_url,
following_url,
gists_url,
starred_url,
subscriptions_url,
organizations_url,
repos_url,
events_url,
received_events_url,
type,
site_admin,
name,
company,
blog,
location,
email,
hireable,
bio,
public_repos,
public_gists,
followers,
following,
created_at,
updated_at
]
);
});
答案 0 :(得分:1)
与其对对象进行SQL和转换的硬编码,只需将字段的预期顺序保留为常数,然后将解析的JSON中的值映射到values
的数组中即可。
您还可以从相同的常量列表生成SQL和占位符:
// Keep a list for the fields where order is important
const fieldOrder = [
'login',
'id',
'node_id',
'avatar_url',
'gravatar_id',
'url',
'html_url',
'followers_url',
'following_url',
'gists_url',
'starred_url',
'subscriptions_url',
'organizations_url',
'repos_url',
'events_url',
'received_events_url',
'type',
'site_admin',
'name',
'company',
'blog',
'location',
'email',
'hireable',
'bio',
'public_repos',
'public_gists',
'followers',
'following',
'created_at',
'updated_at'
];
// Parse your content in the same place
let result = JSON.parse(data);
// Extract the value by the same key names
let values = fieldOrder.map(k => result[k]);
// Generate the statement rather than hardcoding
let sql = `INSERT into user (${fieldOrder.join(',')}) values(${fieldOrder.map(e => '?').join(',')})`
// pass these arguments to your function
db.query(sql, values);
这非常简单,实质上是许多ORM库在其功能实现的外观下为您所做的事情。
请注意,其中的许多部分都是通用且可重用的,这是此类库实现的另一功能。
因此,您真正不能避免的一个“邪恶” 是保留该字段列表,因为顺序可能很重要,并且这是清除所有意外数据的合理方法。
“便宜又讨厌” 的方式可能是:
let fieldOrder = Object.keys(result);
甚至:
let [fieldOrder, values] = Object.entries(result);
但这并不能真正控制您在data
中发送的有效内容,而且还可能造成破坏。
无论如何,即使在代码中的某个位置保留一个恒定的列表,通过将列出相同字段名称的所有位置都移动到单个列表中,这与当前列表相比是一个很大的减少。