程序问答   发布时间:2022-06-01  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了为 app.POST 服务器创建多个 SQL 语句查询大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。

如何解决为 app.pOST 服务器创建多个 SQL 语句查询?

开发过程中遇到为 app.pOST 服务器创建多个 SQL 语句查询的问题如何解决?下面主要结合日常开发的经验,给出你关于为 app.pOST 服务器创建多个 SQL 语句查询的解决方法建议,希望对你解决为 app.pOST 服务器创建多个 SQL 语句查询有所启发或帮助;

我目前一直在尝试在 server.Js 中的 app.post 函数上运行多个插入 sql 语句。这是用于视频游戏数据库。 首先,我正在尝试已注释掉的方法,但在尝试插入新游戏时会收到一条错误消息,提示“列索引超出范围”。然后我尝试了未注释的函数,看看我是否可以一次执行一个,但我现在收到的错误是“在将标题发送到客户端后无法设置标题”@H_618_3@

<input type="text" name="barcode-input" autofocus style="display:none" />

<div ID="current"></div>
//Server.Js

// vIDeogames.insertNewGame(data.gametitle,data.year,data.genre,data.publisher,data.developer,data.platformCB)
    //     .then(insertNew => {
    //         res.Json({
    //             "message":`success! New Game Added`,//             "data":insertNew
    //         })
    //     })
    //     .catch(err => {
    //         res.status(400).Json({"error":err.messagE})
    //     })

    vIDeogames.insertNewGame(data.gametitle,data.genrE)
        .then(insertNew => {
            res.Json({
                "message":`success! New Game Added`,"data":insertNew
            })
        })
        .catch(err => {
            res.status(400).Json({"error":err.messagE})
        })
    vIDeogames.updateNewGame(data.gametitle,data.platformCB)
        .then(insertNew => {
            res.Json({
                "message":`success! updated exKey`,"data":insertNew
            })
        })
        .catch(err => {
            res.status(400).Json({"error":err.messagE})
        })

“this.all”调用这个函数@H_618_3@

//database.Js

// insertNewGame(gtitle,gYear,gGenre,pPub,dDev,pfCB){
    //     return this.all(
    //         "INSERT INTO Games (g_title,g_year,g_genrE) VALUES(?,?,?); " + 
    //         "updatE Games SET g_exkey = (SELECT pf_exkey FROM Platform WHERE pf_system = ?) WHERE g_title = ?; " +
    //         "INSERT INTO Publisher (p_Name) VALUES(?); " +
    //         "INSERT INTO Developer (d_Name) VALUES(?); " +
    //         "INSERT INTO Contracts (c_gamEID,c_pubkey,c_devkey) " + 
    //             "SELECT g_gamEID,p_pubkey,d_devkey " +
    //                     "FROM Games,Publisher,Developer " +
    //                     "WHERE g_title = ? AND " +
    //                         "p_name = ? AND " +
    //                         "d_name = ?",[gtitle,pfCB,gtitle,dDev])
    // }

    insertNewGame(gtitle,gGenrE){
        return this.all(
            "INSERT INTO Games (g_title,?)",gGenre]
        )
    }
    updateNewGame(gtitle,pCB){
        return this.all(
            "updatE Games SET g_exkey = (SELECT pf_exkey FROM Platform WHERE pf_system = ?) WHERE g_title = ?",pCB]
        )
    }
    insertPublisher(pPub){

澄清一下,我从 HTML 表单数据部分获取所有数据,然后将其设置为您在此处看到的内容,“data.publisher、data.developer,...”@H_618_3@

解决方法

所以我想我明白了。我不断收到“在将标头发送到客户端后无法设置标头”的原因是因为我在每次函数调用后都在尝试使用 res.json。我为此所做的修复是使用 res.json 删除所有 .then 语句并将其移至仅最后一个函数。 res.json 基本上是请求调用的结束语句,这就是发送标头的原因,之后无法再次执行。这是有效的解决方案@H_618_3@

videogames.insertNewGame(data.gametitle,data.year,data.genrE)
        .catch(err => {
            res.status(400).json({"error":err.messagE})
        })
videogames.updateNewGame(data.gametitle,data.platformCB)
    .catch(err => {
        res.status(400).json({"error":err.messagE})
    })
videogames.insertPublisher(data.publisher)
    .catch(err => {
        res.status(400).json({"error":err.messagE})
    })
videogames.insertDeveloper(data.developer)
    .catch(err => {
        res.status(400).json({"error":err.messagE})
    })
videogames.insertContract(data.gametitle,data.publisher,data.developer)
    .then(() => {
        res.json({
            "message":`success! inserted new game`,"data":data.gametitle
        })            
    })
    .catch(err => {
        res.status(400).json({"error":err.messagE})
    })

大佬总结

以上是大佬教程为你收集整理的为 app.POST 服务器创建多个 SQL 语句查询全部内容,希望文章能够帮你解决为 app.POST 服务器创建多个 SQL 语句查询所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。
标签: