错误:缺少表“脚本”的FROM子句条目

时间:2019-10-08 08:31:32

标签: javascript sql node.js postgresql

我有一条路线

app.get('/chats/:user_id', async (req, res) => {
    const user_id = req.params.user_id;
    const room = new Room(user_id);
    const userRooms = (await db.query(room.getUserRooms())).rows;
    console.log(userRooms);
    res.render('index', { rooms: userRooms });
});

我在其中请求用户聊天。

class Room {
    constructor(id) {
        this.user_id = id;
    }

    getUserRooms() {
        const sql = `SELECT R.CHAT_ID, R.RECEIVER, R.SENDER FROM CHATS AS R WHERE R.RECEIVER = ${this.user_id} OR R.SENDER = ${this.user_id}`;
        return sql;
    }
}

module.exports = Room;

我得到一个错误:

  

(节点:6944)UnhandledPromiseRejectionWarning:错误:缺少表“脚本”的FROM子句条目       在Connection.parseE(C:\ Users \ User \ Documents \ Realtime-Chat-App-With-Rooms \ node_modules \ pg \ lib \ connection.js:604:11)       在Connection.parseMessage(C:\ Users \ User \ Documents \ Realtime-Chat-App-With-Rooms \ node_modules \ pg \ lib \ connection.js:401:19)       在TLSSocket。 (C:\ Users \ User \ Documents \ Real-Chat-App-With-Rooms \ node_modules \ pg \ lib \ connection.js:121:22)       在TLSSocket.emit(events.js:189:13)       在addChunk(_stream_visible.js:284:12)       在readAddChunk(_stream_visible.js:265:11)       在TLSSocket.Readable.push(_stream_visible.js:220:10)       在TLSWrap.onStreamRead [阅读时](internal / stream_base_commons.js:94:17)   (节点:6944)UnhandledPromiseRejectionWarning:未处理的承诺拒绝。引发此错误的原因可能是抛出了一个没有catch块的异步函数,或者是拒绝了一个.catch()无法处理的承诺。 (拒绝ID:10)

我不明白为什么会发生此错误。

我在哪里弄错了?

我按照他们的要求const userRooms = await db.query(room.getUserRooms())

Result {
  command: 'SELECT',
  rowCount: 2,
  oid: null,
  rows:
   [ { chat_id: 1, receiver: 51, sender: 71 },
     { chat_id: 2, receiver: 71, sender: 51 } ],
  fields:
   [ Field {
       name: 'chat_id',
       tableID: 42149012,
       columnID: 1,
       dataTypeID: 23,
       dataTypeSize: 4,
       dataTypeModifier: -1,
       format: 'text' },
     Field {
       name: 'receiver',
       tableID: 42149012,
       columnID: 2,
       dataTypeID: 23,
       dataTypeSize: 4,
       dataTypeModifier: -1,
       format: 'text' },
     Field {
       name: 'sender',
       tableID: 42149012,
       columnID: 3,
       dataTypeID: 23,
       dataTypeSize: 4,
       dataTypeModifier: -1,
       format: 'text' } ],
  _parsers:
   [ [Function: parseInteger],
     [Function: parseInteger],
     [Function: parseInteger] ],
  _types:
   TypeOverrides {
     _types:
      { getTypeParser: [Function: getTypeParser],
        setTypeParser: [Function: setTypeParser],
        arrayParser: [Object],
        builtins: [Object] },
     text: {},
     binary: {} },
  RowCtor: null,
  rowAsArray: false }
[ { chat_id: 1, receiver: 51, sender: 71 },
  { chat_id: 2, receiver: 71, sender: 51 } ]

0 个答案:

没有答案