Javascript 如何获取测试所需的insertId
我正在做一个没有框架或ORM的后端应用程序,我正在使用mysql2包进行节点研究,当我要向表中添加一本书时,我不能这样做,因为错误Javascript 如何获取测试所需的insertId,javascript,mysql,node.js,oop,jestjs,Javascript,Mysql,Node.js,Oop,Jestjs,我正在做一个没有框架或ORM的后端应用程序,我正在使用mysql2包进行节点研究,当我要向表中添加一本书时,我不能这样做,因为错误列author\u id不能为null,尽管它已填充。通过研究,我明白我需要获取作者和bookDAO的插入id,但我做不到。你能帮我吗 安托尔多 import Author from "../models/Author.js" export default class AuthorDAO { constructor(conn) { t
列author\u id不能为null
,尽管它已填充。通过研究,我明白我需要获取作者
和bookDAO的插入id,但我做不到。你能帮我吗
安托尔多
import Author from "../models/Author.js"
export default class AuthorDAO {
constructor(conn) {
this._conn = conn
}
async add(author) {
if (!(author instanceof Author)) {
throw new Error(`The object is not an author type`)
}
try {
const authors = await this._conn.query(`SELECT * FROM author`)
if (authors.some(a => a.email === author.email)) {
throw new Error(`The author already exists`)
}
await this._conn.query(`INSERT INTO author (name, email) VALUES (?, ?)`, [author.name, author.email])
} catch (error) {
throw new Error(error)
}
}
类作者
export default class Author {
constructor(name, email) {
this.name = name
this.email = email
this.date = new Date()
}
set name(name) {
if (isEmpty(name) || isNull(name)) throw new Error(`The name field needs to be filled`)
this._name = name
}
set email(email) {
if (isEmpty(email) || !email.match(emailFormat))
throw new Error(`The email field must be filled in with a valid format`)
this._email = email
}
get name() {
return this._name
}
get email() {
return this._email
}
}
藏书刀
import Book from "../models/Book.js"
export default class BookDAO {
constructor(conn) {
this._conn = conn
}
async add(book) {
if (!(book instanceof Book)) {
throw new Error(`The Object is not of the Book type`)
}
try {
const books = await this._conn.query(`SELECT * FROM book`)
if(books.some(b => b.title === book.title)){
throw new Error(`There is already a book registration with that title!`)
}
await this._conn.query(`INSERT INTO book (
author_id, category_id, title, resume, summary, number_of_pages,
isbn, edition, price) VALUES
(?, ?, ?, ?, ?, ?, ?, ?, ?)`, [book.author.id, book.category.id, book.title, book.title, book.summary, book.number_of_pages,
book.isbn, book.edition, book.price])
} catch(error){
throw new Error(error)
}
}
开玩笑地测试
it("Successfully adding a book", async () => {
const categoryDAO = new CategoryDAO(conn)
const authorDAO = new AuthorDAO(conn)
const bookDAO = new BookDAO(conn)
const author = new Author("Ana", "Ana@gmail.com")
const category = new Category("Design")
await categoryDAO.add(category)
await authorDAO.add(author)
const book = new Book(
author,
category,
"Design UX/UI",
"This book is about Design",
"Summary",
40,
"978-85-13196-08-9",
50,
"12"
)
await bookDAO.add(book)
expect(book).toBeDefined()
})
桌子
const table_author = `CREATE TABLE IF NOT EXISTS author(
id INTEGER NOT NULL AUTO_INCREMENT PRIMARY KEY,
name VARCHAR(50) NOT NULL,
email VARCHAR(50) NOT NULL UNIQUE KEY,
date TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP
)`
const table_book = `CREATE TABLE IF NOT EXISTS book (
id INTEGER NOT NULL AUTO_INCREMENT PRIMARY KEY,
title VARCHAR(50) NOT NULL UNIQUE KEY,
resume VARCHAR(500) NOT NULL,
summary VARCHAR(500) NOT NULL,
number_of_pages INTEGER UNSIGNED NOT NULL,
isbn VARCHAR(17) NOT NULL UNIQUE KEY,
author_id INTEGER NOT NULL,
category_id INTEGER NOT NULL,
edition INTEGER UNSIGNED NOT NULL,
price FLOAT UNSIGNED NOT NULL,
time_registration TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
FOREIGN KEY (author_id) REFERENCES author(id),
FOREIGN KEY (category_id) REFERENCES category(id)
ON DELETE CASCADE
)`
日志
在上面的帮助下,我解决了这个问题。由于我的
query
方法只接收到两个参数,callbak函数不起作用,因此我按如下方式执行:
async add(author) {
if (!(author instanceof Author)) {
throw new Error(`The object is not an author type`)
}
try {
const authors = await this._conn.query(`SELECT * FROM author`)
if (authors.some(a => a.email === author.email)) {
throw new Error(`The author already exists`)
}
const result = await this._conn.query(
"INSERT INTO author (name, email) VALUES (?, ?)",
[author.name, author.email])
author.id = result.insertId
console.log(result.insertId)
} catch (error) {
throw new Error(error)
}
}
类作者没有id,但正在遍历。。查询需要
book.author.id
,如果调试时只需要book.author.name
和book.author.email
插入作者后,是否需要检索并将其填充到author
变量中,因此,当您尝试插入图书记录时,它是可访问的?您需要从等待此操作返回insertId。\u conn.query(`insert-INTO-author`
(或整个author对象),即const-author=wait-authorDAO.add(new-author('Ana','Ana@gmail.com"))
@Laura是的,那将是正确的地方,因为ID是在那里生成的。我之前评论中的链接显示了一个如何执行该操作的示例。@Laura否,您必须作为插入
的一部分,而不是选择
。尝试等待此操作。_conn.query('INSERT-INTO-author(name,email)value(?,)'),[author.name,author.email],函数(err,result,fields){author.id=result.insertId})
。
async add(author) {
if (!(author instanceof Author)) {
throw new Error(`The object is not an author type`)
}
try {
const authors = await this._conn.query(`SELECT * FROM author`)
if (authors.some(a => a.email === author.email)) {
throw new Error(`The author already exists`)
}
const result = await this._conn.query(
"INSERT INTO author (name, email) VALUES (?, ?)",
[author.name, author.email])
author.id = result.insertId
console.log(result.insertId)
} catch (error) {
throw new Error(error)
}
}