首页 > 解决方案 > first_error_message: '语法错误 - 在''

问题描述

我正在尝试更新 Couchbase Bucket 上特定 json 文档中的数组。基本上我想要做的是向数组中插入一个新对象。当我尝试对数据库本身进行以下查询时,查询运行良好,

UPDATE BUCKETNAME SET appl[3]= {
      'appl_ads_id': 'testtttt',
      'appl_bnd_lvl_cd': 30,
      'appl_desire_skill_tx': [
        "couchbase",
        "nodejs"
      ],
      'appl_email_tx': "TESTING123@gmail.com",
      'appl_full_nm': "TEST TESTING",
      'appl_id': "4444",
      'appl_ldr_apprvd': true,
      'appl_locat_tx': "ALABAMA",
      'appl_role_nm': "Engineer III",
      'appl_status': "PENDING"}
          where opp_id= 1 and opp_nm= "Couchbase Management Project" RETURNING *;

但是当我尝试将该查询写入我的javascript代码时,出现以下错误:

QueryErrorContext {
     first_error_code: 3000,
     first_error_message: 'syntax error - at '',
     statement:
      'UPDATE `BUCKETNAME` SET appl[3]= {'appl_ads_id': 'albm111', 'appl_bnd_lvl_cd': 30, 'appl_desire_skill_tx': ['couchbase', 'nodejs'],'appl_email_tx': 'zzzzzzz','appl_full_nm': 'z Z','appl_id': '222222','appl_ldr_apprvd': false, 'appl_locat_tx': 'ALABAMA','appl_role_nm': 'Engineer III','appl_status': 'Test'} where opp_id= $ID and opp_nm= $NAME  RETURNING *;',
     client_context_id: '54f4f683f306b4a5',
     parameters: '',
     http_response_code: 400,
     http_response_body: '' } }

请在下面找到我如何在我的 nodejs 代码上编写 N1ql 查询:

async function queryResults() {
  const query = "UPDATE BUCKETNAME SET appl[3]= {'appl_ads_id': 'mfern685', 'appl_bnd_lvl_cd': 30, 'appl_desire_skill_tx': ['couchbase', 'nodejs'],'appl_email_tx': 'zzzzzzz','appl_full_nm': 'z Z','appl_id': '222222','appl_ldr_apprvd': false, 'appl_locat_tx': 'Sunrise','appl_role_nm': 'Engineer III','appl_status': 'Test'} where opp_id= $ID and opp_nm= $NAME  RETURNING *;"
  
  
  const options = { parameters: { ID: 1, NAME: 'Couchbase Management Project' } }
  try {
    let results = await cluster.query(query,options);
    results.rows.forEach((row) => {
      console.log('Query row: ', row)
    })
    return results
  } catch (error) {
    console.error('Query failed: ', error)
  }
}
​

标签: node.jscouchbasen1ql

解决方案


查询的第二部分应使用标准 SQL 语法:

update my_bucket set myArray[3] = {teste: 'something'}, appl_email_tx =  'TESTING123@gmail.com';

推荐阅读