Knex.js返回插入的记录ID



我正在quoteNotes表中插入一个注释。当我插入它并控制台记录res.时,它不会给我插入笔记的记录。

router.post('/:id/notes', (req, res) => {
const {id} = req.params;
const note = req.body;
note.quote_id = id
// if(note.note === "") return res.status(422)
//   .json({message: 'Note cannot be empty'});
Quotes.addNote(note).then((quoteNote) => {
console.log(quoteNote)
res.status(200).json(quoteNote);
});
});

console.log=>

Result {
command: 'INSERT',
rowCount: 1,
oid: 0,
rows: [],
fields: [],
_parsers: undefined,
_types: TypeOverrides {
_types: {
getTypeParser: [Function: getTypeParser],
setTypeParser: [Function: setTypeParser],
arrayParser: [Object],
builtins: [Object]
},
text: {},
binary: {}
},
RowCtor: null,
rowAsArray: false
}

想明白了。

需要将CCD_ 1添加到查询中。

function addNote(data) {
return db('quote_notes')
.insert(data)
.returning('id');
}

在我使用mysql的情况下,await insert({...})返回了一个id为单个元素的数组。通过使用returning('id'),我得到了一个错误,说mysql不支持它。