【问题标题】:webapi call to nodejs to postgres with multiple placeholders not workingwebapi调用nodejs到postgres,多个占位符不起作用
【发布时间】:2018-08-10 11:49:12
【问题描述】:

我有两张表recipemaster 和 Ingredientmaster。

配方表

recipe_code |recipe_title |
------------|-------------|
1           |    one      |
2           |    two      |
3           |    three    |
4           |    four     |

成分主表

ingredient_code |recipe_code |ingredient_detail |
----------------|------------|------------------|
                |            |                  | 

在前端反应中,我使用选择框根据从 recipemaster 表中选择的 recipe_title 输入成分详细信息,我的任务是从 recipemaster 表中选择 recipe_title 并且应该根据 recipe_title 选择特定的 recipe_code 并填充该特定的 recipe_code在配料表中。当然,component_detail 也是一起输入的。 Ingredient_code 是自动递增的,因此无需担心。

我尝试了什么:-

app.post('/api/new-ingre',function(request,response){
    console.log('Ingredients connected to db')
    var recipe_code;
    var recipe_title = request.body.recipe_title;
    var recipe_text = request.body.recipe_text;
    let values = [recipe_title,recipe_text];
    pool.connect((err,db,done)=>{
        if(err){
            return response.status(400).send(err);
        }
        else{
            console.log(request.body.recipe_title)
            console.log(request.body.recipe_text)
            db.query('INSERT INTO public.ingredientmaster(recipe_code,ingredient_detail=?) select recipe_code from recipemaster where recipe_title = ?',[recipe_text,recipe_title],(err,table)=>{
                done();
                if(err){
                    return response.status(400).send(err);
                }
                else{
                    console.log('INGREDIENT DATA INSERTED');
                    response.status(201).send({message:'Ingredient data inserted'});
                }
            })
        }
    })
});

查询在数据库中执行时有效。但是当这种类型的复杂查询出现时,我不知道如何在 nodejs 中使用此查询。 帮助表示赞赏。谢谢。

【问题讨论】:

  • 你使用的是什么数据库和库?
  • Postgres 和 npm 库,获取请求
  • 然后尝试使用$1$2 作为占位符
  • 我也这样做了,但没有成功
  • 为什么不使用 ORM?并且路由器不应满足任何业务逻辑或数据库事务。

标签: node.js postgresql


【解决方案1】:

好的,我在开始实施时遇到了类似的问题,这是因为我对 postgres 没有足够的了解,所以我的建议是:

  1. 我正在使用 pg-promise 库
  2. 像这样创建一个连接单例文件 (mydb.js)

    'use strict';
    let pgp = require('pg-promise')();
    //let db = pgp(config.postgres);
    module.exports.db = db;
    
  3. 使用以前的文件连接到您的 app.js(或等效的主文件)上的 PostgreSQL,如下所示:

    import { db } from './mydb';
    //.... more imports and initialization lines
    //....
    //I use this to validate postgres connection only on app.js
    db.proc('version')
    .then(() => {
      console.log('Postgres Connected.... ');
    })
    .catch(err => {
      console.log('Postgres connection error:');
      console.error(err);
      process.exit(-1);
    });
    
  4. 在您的控制器上使用此代码或类似代码:

    import { db } from './mydb';
    //...more code
    app.post('/api/new-ingre',function(request,response){
       let sqlInsert = `INSERT INTO ......`;
       db.any(sqlInsert, [])
       .then(()=>{
           console.log('INGREDIENT DATA INSERTED');
           response.status(201).send({message:'Ingredient data inserted'});
        })
       .catch((err)=>{
           response.status(400).send(err);
       })
    });
    

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-09-28
    • 2014-03-04
    • 2015-03-24
    • 2017-06-29
    • 1970-01-01
    • 2018-02-19
    • 2018-03-05
    相关资源
    最近更新 更多