书架 js 集合

Bookshelf js collection

本文关键字:集合 js 书架      更新时间:2023-09-26

我正在尝试使用书架集合并返回整个表

< TypeError: Cannot read property 'apply' of undefined
<     at CollectionBase (/projects/twitter/node_modules/bookshelf/lib/base/collection.js:30:18)
<     at child [as constructor] (/projects/twitter/node_modules/bookshelf/lib/extend.js:15:14)
<     at new child (/projects/twitter/node_modules/bookshelf/lib/extend.js:15:14)
<     at Function.collection (/projects/twitter/node_modules/bookshelf/lib/bookshelf.js:110:14)
<     at all (/projects/twitter/node_modules/bookshelf/lib/model.js:703:39)
<     at fetchAll (/projects/twitter/node_modules/bookshelf/lib/model.js:768:27)

这是我的模型然后收藏

var Users_Data_At_Time = bookshelf.Model.extend({
    tableName: 'Users_Data_At_Time',
    Users: function(){
        return this.belongsTo(Users);
    },
    Tweets_At_Time: function(){
        return this.hasMany(Tweets_At_Time);
    }
});
var Users_Data_At_Time_Collection = bookshelf.Collection.extend({
model: Users_Data_At_Time
});

我尝试使用 .fetchAll 调用集合和模型

这是我尝试使用模型的地方

new Users_Data_At_Time().fetchAll()
.then(function (users) {
        console.log(users);
    }).catch(function (error) {
        console.log(error);
    })

我已经看过几个例子,似乎这是正确的方法,但我仍然收到该错误。

任何想法或想法都会有很大的帮助

我想

在所有书架上执行此操作,但作为一种解决方法,我发现了这个溢出帖子,它使用书架中knex的原始查询来选择所有内容。

我不确定这是否是唯一的解决方案,如果有其他人,请告诉我

您的问题可能是文件/模型的命名。表名snake_cased在您的数据库中,所以这显然没问题。但是模型名称需要是PascalCased(又名UpperCamelCase(。反向适用于您的文件,因此应该snake_cased。

看看这个家伙,特别是最后一段:https://www.npmjs.com/package/bookshelf-model