通过express发布到mysql数据库时出错

时间:2018-02-06 02:25:12

标签: mysql sql node.js express

我从我的反应前端发送一个帖子到我的快递api。我一直收到对象到我的模型,但是我在尝试插入数据时遇到错误。

Customer.newUser = (customer) => {
    return db.then(function(conn){
        return conn.query(
            `INSERT INTO customers (FirstName, LastName, Age) VALUES (${customer.FirstName}, ${customer.LastName}, ${customer.Age})`
        );
    })
}

所有变量和数据都在正确的位置,但我不断收到此错误

Port : 3001
{ first: 'Joey', last: 'dip', age: 29 } 'idjfosjdsofij'
{ Error: ER_BAD_FIELD_ERROR: Unknown column 'Joey' in 'field list'
    at Query.Sequence._packetToError (/Users/joey.diperi/Documents/websites/lorea_elvive_quiz/node_modules/mysql/lib/protocol/sequences/Sequence.js:52:14)
    at Query.ErrorPacket (/Users/joey.diperi/Documents/websites/lorea_elvive_quiz/node_modules/mysql/lib/protocol/sequences/Query.js:77:18)
    at Protocol._parsePacket (/Users/joey.diperi/Documents/websites/lorea_elvive_quiz/node_modules/mysql/lib/protocol/Protocol.js:279:23)
    at Parser.write (/Users/joey.diperi/Documents/websites/lorea_elvive_quiz/node_modules/mysql/lib/protocol/Parser.js:76:12)
    at Protocol.write (/Users/joey.diperi/Documents/websites/lorea_elvive_quiz/node_modules/mysql/lib/protocol/Protocol.js:39:16)
    at Socket.<anonymous> (/Users/joey.diperi/Documents/websites/lorea_elvive_quiz/node_modules/mysql/lib/Connection.js:103:28)
    at emitOne (events.js:116:13)
    at Socket.emit (events.js:211:7)
    at addChunk (_stream_readable.js:263:12)
    at readableAddChunk (_stream_readable.js:250:11)
    at Socket.Readable.push (_stream_readable.js:208:10)
    at TCP.onread (net.js:594:20)
    --------------------
    at Protocol._enqueue (/Users/joey.diperi/Documents/websites/lorea_elvive_quiz/node_modules/mysql/lib/protocol/Protocol.js:145:48)
    at Connection.query (/Users/joey.diperi/Documents/websites/lorea_elvive_quiz/node_modules/mysql/lib/Connection.js:208:25)
    at /Users/joey.diperi/Documents/websites/lorea_elvive_quiz/node_modules/promise-mysql/lib/helper.js:16:24
    at Promise._execute (/Users/joey.diperi/Documents/websites/lorea_elvive_quiz/node_modules/bluebird/js/release/debuggability.js:303:9)
    at Promise._resolveFromExecutor (/Users/joey.diperi/Documents/websites/lorea_elvive_quiz/node_modules/bluebird/js/release/promise.js:483:18)
    at new Promise (/Users/joey.diperi/Documents/websites/lorea_elvive_quiz/node_modules/bluebird/js/release/promise.js:79:10)
    at Connection.promiseCallback (/Users/joey.diperi/Documents/websites/lorea_elvive_quiz/node_modules/promise-mysql/lib/helper.js:6:10)
    at connection.query (/Users/joey.diperi/Documents/websites/lorea_elvive_quiz/node_modules/promise-mysql/lib/connection.js:64:28)
    at /Users/joey.diperi/Documents/websites/lorea_elvive_quiz/models/customer.js:13:21
    at tryCatcher (/Users/joey.diperi/Documents/websites/lorea_elvive_quiz/node_modules/bluebird/js/release/util.js:16:23)
    at Promise._settlePromiseFromHandler (/Users/joey.diperi/Documents/websites/lorea_elvive_quiz/node_modules/bluebird/js/release/promise.js:512:31)
    at Promise._settlePromise (/Users/joey.diperi/Documents/websites/lorea_elvive_quiz/node_modules/bluebird/js/release/promise.js:569:18)
    at Promise._settlePromiseCtx (/Users/joey.diperi/Documents/websites/lorea_elvive_quiz/node_modules/bluebird/js/release/promise.js:606:10)
    at Async._drainQueue (/Users/joey.diperi/Documents/websites/lorea_elvive_quiz/node_modules/bluebird/js/release/async.js:138:12)
    at Async._drainQueues (/Users/joey.diperi/Documents/websites/lorea_elvive_quiz/node_modules/bluebird/js/release/async.js:143:10)
    at Immediate.Async.drainQueues [as _onImmediate] (/Users/joey.diperi/Documents/websites/lorea_elvive_quiz/node_modules/bluebird/js/release/async.js:17:14)
  code: 'ER_BAD_FIELD_ERROR',
  errno: 1054,
  sqlMessage: 'Unknown column \'Joey\' in \'field list\'',
  sqlState: '42S22',
  index: 0,
  sql: 'INSERT INTO customers (FirstName, LastName, Age) VALUES (Joey, dip, 29)' }
POST /customer 500 35.348 ms - 212

2 个答案:

答案 0 :(得分:0)

您的语法不正确。你获得它的方式是DB将其作为一个列而不是一个值来读取。

&#39; ER_BAD_FIELD_ERROR&#39;,错误号:1054,sqlMessage:&#39;未知列\&#39; Joey \&#39;在\'#字段列表\&#39;&#39;,sqlState:&#39; 42S22&#39;,index:0,sql:&#39; INSERT INTO customers(FirstName,LastName,Age)VALUES (Joey,dip,29)&#39; }

请看这个例子:java sql insert

答案 1 :(得分:0)

确保查询中的每个值都用单引号括起来。检查下面的问题代码的说明或修改。

Customer.newUser = (customer) => {
  return db.then(function(conn){
    return conn.query(
      `INSERT INTO customers (FirstName, LastName, Age) VALUES 
      ('${customer.FirstName}', '${customer.LastName}', '${customer.Age}')`
    );
  })
}