【发布时间】:2020-03-16 08:34:18
【问题描述】:
我有一个从昨天开始一直无法解决的问题,这是我将在下面放置的代码,但我想精确,我有 2 个表,wp 帖子和 wp 帖子元,我尝试获取帖子的最后一个 id,然后根据它插入,因为我使用了 promises,但是最后一个函数“insertlink”没有返回最后一个 id,它说它没有定义,我不明白,谢谢。
function insertarpost()
{
return new Promise(function(resolve, reject) {
var sql = "INSERT INTO `wp_posts` ( `post_author`, `post_date`, `post_date_gmt`, `post_content`, `post_title`,`post_excerpt`, `post_status`,`comment_status`, `ping_status`,`post_password`, `post_name`,`to_ping`,`pinged`, `post_modified`, `post_modified_gmt`,`post_content_filtered`,`post_parent`, `guid`,`menu_order`, `post_type`, `post_mime_type`,`comment_count`) VALUES (1, '"+fhoy+"', '"+fhoy+"', '"+description+"', '"+title+"','', 'publish', 'open', 'closed','','"+slug+"','','', '"+fhoy+"', '"+fhoy+"','', '0','','0', 'movies','' ,0);";
con.query(sql, function (err, result) {
if (err) throw err;
resolve(result);
console.log("1 registro insertado");
});
});
}
insertarpost().then(obtenerultimopostid);
function obtenerultimopostid() {
return new Promise(function(resolve, reject) {
con.query("SELECT ID FROM wp_posts ORDER BY ID DESC LIMIT 0,1;", function (err, result, fields) {
console.log(result);
var sql = "INSERT INTO wp_postmeta (post_id, meta_key, meta_value) VALUES ("+result[0].ID+",'runtime', '"+runtime+"'),("+result[0].ID+",'original_title','"+titleoriginal+"'),("+result[0].ID+",'Rated','"+cpgrated+"'),("+result[0].ID+",'Country', '"+country+"'),("+result[0].ID+",'date', '"+datemovie+"'),("+result[0].ID+",'imdbRating', '"+repimdb+"'),("+result[0].ID+",'vote_average', '"+reptmdb+"'),("+result[0].ID+",'imdbVotes', '"+quantimdb+"'),("+result[0].ID+",'vote_count', '"+quanttmdb+"'),("+result[0].ID+",'tagline', '"+tagline+"'),("+result[0].ID+",'dt_poster', '"+poster+"'),("+result[0].ID+",'dt_backdrop', '"+backdrop+"'),("+result[0].ID+",'imagenes', '"+backdrops+"'),("+result[0].ID+",'dt_cast', '"+textimgreparto+"'),("+result[0].ID+",'dt_dir', '"+textimgreparto2+"');";
con.query(sql, function (err, result) {
if (err) throw err;
resolve(result);
console.log("1 registro wpmeta insertado");
});
});
});
}
obtenerultimopostid().then(insertarlink);
var idultimomovie='';
function insertarlink() {
var sql ="SELECT ID FROM wp_posts ORDER BY ID DESC LIMIT 0,1;";
con.query(sql, function (err, result) {
if (err) throw err;
idultimomovie=result[0].ID;
console.log(idultimomovie);
});
for(var i=0;i<getData.length;i++) {
var source=getData[i]['source'];
var text =getData[i]['text'];
var quality = getData[i]['quality'];
var sql = "INSERT INTO `wp_posts` ( `post_author`, `post_date`, `post_date_gmt`, `post_content`, `post_title`,`post_excerpt`, `post_status`,`comment_status`, `ping_status`,`post_password`, `post_name`,`to_ping`,`pinged`, `post_modified`, `post_modified_gmt`,`post_content_filtered`,`post_parent`, `guid`,`menu_order`, `post_type`, `post_mime_type`,`comment_count`) VALUES (1, '"+fhoy+"', '"+fhoy+"', '', '"+make+"','', 'publish', 'closed', 'closed','','"+make+"','','', '"+fhoy+"', '"+fhoy+"','', '"+idultimomovie+"','','0', 'dt_links','' ,0);";
con.query(sql, function (err, result) {
if (err) throw err;
console.log("1 registro link insertado");
});
}
}
【问题讨论】:
-
我不明白,当我删除函数'insertlink'或留下一个简单的'console.log()'消息时我得到一个错误,好像有什么问题只需命名即可。
-
node.js 中是否禁止格式化?