微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

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

如何解决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 举报,一经查实,本站将立刻删除。