【问题标题】:pg-promise helpers.update not workingpg-promise helpers.update 不工作
【发布时间】:2017-10-26 16:54:28
【问题描述】:

我无法让helpers.update 方法工作。我几乎 100% 确定这是我忽略的简单事情。 以下是代码中涉及的表格中的列:

  • user_social_profile_id
  • user_id
  • user_social_profile_platform
  • user_social_profile_link

以下是我对ColumnSet 的定义。 (请注意,我添加了一些实际代码中没有的间距。我只是讨厌滚动这些窗口。)

const usersSocialProfiles = new pgp.helpers.ColumnSet(
  [ 
    'user_id', 
    'user_social_profile_platform',
    'user_social_profile_link'
  ],
  {table: 'users_social_profiles'}
);
const usersSocialProfilesUpdate = new pgp.helpers.ColumnSet(
  [ 
    '?user_social_profile_id',
    '?user_id',
    'user_social_profile_platform',
    'user_social_profile_link'
  ],
  {table:'users_social_profiles'}
);

然后是一个函数来切换我想使用哪个ColumnSet

var pgpConstantSet = function(setName){
  if(setName === "usersSocialProfiles")
  {
    return usersSocialProfiles; 
  }
  if(setName === "updateUsersSocialProfiles")
  {
    return usersSocialProfilesUpdate;
  }
}

尝试使用 ColumnSet

  var profiles = await pgp.helpers.update(userData.socialProfiles, 
  pgConstantSet("usersSocialProfiles"))+ 
  " WHERE t.user_social_profile_id=v.user_social_profile_id";
  console.log(profiles);

  return res.status(201).end();

以及 console.log(profiles) 打印的内容

将“users_social_profiles”更新为“t”集 "user_id"="v"."user_id","user_social_profile_platform"="v"."user_social_profile_platform","user_social_profile_link"="v"."user_social_profile_link" FROM (VALUES(51,'facebook','http://w.sefvfaboklink.com')) AS "v"("user_id","user_social_profile_platform","user_social_profile_link") WHERE t.user_social_profile_id=v.user_social_profile_id 补丁 /api/users 201 55.858 毫秒 - -

我应该注意,我使用了我为插入定义的另一个 ColumnSet,就像一个魅力一样。我真的很感谢任何来到这里的人。我知道这有点乱。

【问题讨论】:

    标签: javascript node.js pg-promise


    【解决方案1】:

    helpers 命名空间中的所有类型和方法都用于生成查询,执行它们时您会混淆。

    在您的代码中,您只生成查询,但从不执行它。您应该在调用helpers.update 之前删除await,因为查询生成是同步的,并使用Database 方法之一来执行查询。后者是异步的;)


    另一方面,您的示例声明usersSocialProfilesUpdate 的更好方法:

    const usersSocialProfilesUpdate = usersSocialProfiles.merge([
        '?user_social_profile_id',
        '?user_id'
      ]);
    

    请参阅方法 mergeextend,这有助于避免重新声明相同的列。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-01-02
      • 1970-01-01
      • 1970-01-01
      • 2018-12-10
      • 2019-02-11
      • 2018-02-28
      • 2017-12-07
      • 2017-07-18
      相关资源
      最近更新 更多