Giter Site home page Giter Site logo

Passing data to view... about bookshelf HOT 5 CLOSED

bookshelf avatar bookshelf commented on April 28, 2024
Passing data to view...

from bookshelf.

Comments (5)

tgriesser avatar tgriesser commented on April 28, 2024

Hi @projectfledge - since the database calls are asynchronous, you'll need to use the then method - which is known as a "promise"... resolving with the "books" collection (I believe you meant to put Bookshelf.Collection.extend on the Books line. It will look like this:

var Book = Bookshelf.Model.extend({
    tableName: 'books'
});

var Books = Bookshelf.Collection.extend({
    model: Book
});

app.get('/books', function(req, res) {
   new Books().fetch().then(function(theBooks) {
     res.render('books', {books: theBooks});
   });
});

let me know if that clears that up - you can read a bit more about promises here.

from bookshelf.

tgriesser avatar tgriesser commented on April 28, 2024

I see you altered the original question - are you sure it's not because you have Bookshelf.Model.extend instead of Bookshelf.Collection.extend on that fifth line?

from bookshelf.

tgriesser avatar tgriesser commented on April 28, 2024

@projectfledge - Also, that passes the raw collection to the view (like you'd have in Backbone) - if you just want the json for the view, you'll need to use collection.toJSON()

from bookshelf.

projectfledge avatar projectfledge commented on April 28, 2024

Thank you! The issue was the Bookshelf.Model.Extend whereas it should have been Collection.

I was looking at the collections section of the documentation and saw the accounts example under forge http://bookshelfjs.org/#Collection-forge var Accounts = Bookshelf.Model.extend({

Cheers

from bookshelf.

tgriesser avatar tgriesser commented on April 28, 2024

Ah, thanks for pointing that out - I'll fix it right now.

from bookshelf.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.