JavaScript 中的 fetch() 和 fetchAll() 有什么区别?

问题描述

举个例子,我有这样的功能

async getSlugToId (data) {
 return await categoryModel.where({
   slug: data.slug
 }).fetch()
},

如您所见,它是 bookshelf's 语法,但我想将其写在 knex 上。所以,我做了这样的功能

async getSlugToId (data) {
return knex('categories')
  .where('slug',`${data.slug}`)
  .select('*')
},

而且我真的不知道这个 fetch 是如何工作的,而且,我有这样的 bookshelf 函数,它已在 knex 上重写:

bookshelf

const product = await productModel.query(qb => {
  qb.leftJoin('photos','photos.product_id','products.id')
  qb.leftJoin('products_translations','products_translations.product_id','products.id')
  qb.where('products.slug',data.slug)
  qb.where('products.price','is not',null)
  qb.where('products_translations.type','title')
    qb.where('products.name','not like','%Crypto%')
}).fetch({
  columns: [
    'products.id','products.price','photos.file','products_translations.text','products.is_currency_product','products.product_type','photos.alt_text','products.margin'
  ]
})

然后在 knex 上:

const product = await knex('products')
  .leftJoin('photos','products.id')
  .leftJoin('products_translations','products.id')
  .where('products.slug',data.slug)
  .andWhere('products.price',null)
  .andWhere('products_translations.type','title')
  .andWhere('products.name','%Crypto%')
  .select('products.id','products.margin')

这样对吗? fetchfetchAll 有什么区别?如果 fetchAll数据库获取所有数据,那么为什么我要给它一些列?我想知道什么时候应该使用 fetch,什么时候应该使用 fetchAll

感谢您的回答!

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)