【问题标题】:RequestError: Validation failed for parameter- Invalid bufferRequestError:参数验证失败 - 缓冲区无效
【发布时间】:2018-06-22 14:51:20
【问题描述】:

嗨,我在 node.js 上遇到 mssql 错误,我必须在数据库 sql server 2014 中插入一个包含 base64 文件的字符串,但是当我插入下面的代码时,出现以下错误:RequestError : 参数“Immagine”的验证失败。缓冲区无效。我该如何解决?

async function Reg(IdCantiere, IdUtenteCreazione, Data, Immagine) {

  var ret = true;

  await sql.connect(DbConfig.config);

  try {
    var request = new sql.Request();
    request.input('IdCantiere', sql.Int, IdCantiere);
    request.input('IdUtenteCreazione', sql.Int, IdUtenteCreazione);
    request.input('Immagine', sql.VarBinary(sql.MAX), Immagine);
    request.input('Data', sql.VarChar, Data);
    var query = "insert into Rapporto(IdCantiere,IdUtenteCreazione,NumeroDocumento,Data,Immagine) values(@IDCantiere,@IdUtenteCreazione,( /* Carico il l'ultimo numero  del rappportino di un utente*/ SELECT top 1 NumeroDocumento from Rapporto where Rapporto.IdUtenteCreazione=@IdUtenteCreazione and YEAR(Rapporto.Data)=YEAR(GETDATE()) order by CAST(Rapporto.NumeroDocumento  as int) desc),@Data,@Immagine); SELECT SCOPE_IDENTITY() as valore;";
    var recordset = await request.query(query);
    request = new sql.Request();
    request.input('IdRapporto', sql.Int, recordset.recordset[0].valore);
    recordset = await request.query('Insert into RapportoMobile(IdRapporto) values(@IdRapporto);');
  } catch (err) {
    ret = false;
    console.log("error -> ", err);
  }
  await sql.close();
  return ret;

}

【问题讨论】:

  • 您是否先将保存的内容投射到varbinary
  • 没有@feiiiiii....
  • 然后这样做? cast('something' as varbinary(max))
  • @feiiiiii 我尝试在查询中强制转换,但我有同样的错误

标签: sql sql-server node.js mssql-jdbc


【解决方案1】:

我在尝试将加密值(存储为字符串)放入 VarBinary 时遇到了同样的问题。转换为缓冲区解决了这个问题 - new Buffer(someStringValue)

  const pool = await db.dbConnector();  //Setup mssql connection pool
  const result = await pool.request()
      .input('MerchantId', mssql.Int, merchantId)
      .input('AppKey', mssql.VarChar, applicationKey)  
      .input('AppSecret', mssql.VarBinary, new Buffer(encryptedApplicationSecret))
      .execute('[Security].[Account_Save]');

支持 feiiiiii 的评论让我走上了正确的道路

【讨论】:

  • new Buffer 现在被弃用,取而代之的是 Buffer.from() 和 Buffer.alloc();
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2012-02-26
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-05-16
相关资源
最近更新 更多