【发布时间】:2018-09-29 20:14:53
【问题描述】:
我正在关注 the Node.js + Express tutorial from the Mozilla Developer Network,但我正在使用 MySQL 和 sequelize。
我在Model.count() 调用时遇到错误:
TypeError: Book.count 不是函数 在 bookCount (D:\node-apps\express-library\controllers\bookController.js:9:12)
以下是来自controllers/bookController.js的代码。错误是从Book.count().then(callback); 行触发的。
var Book = require('../models/book');
var Author = require('../models/author');
var async = require('async');
exports.index = function(req, res) {
async.parallel({
bookCount: function(callback) {
Book.count().then(callback);
},
authorCount: function(callback) {
Author.count().then(callback);
}
}, function(err, results) {
console.log(results);
res.render('index', { title: 'Local Library', error: err, data: results });
});
};
我调试了console.log(Book),它返回[function]。以下为models/book.js:
'use strict';
module.exports = (sequelize, DataTypes) => {
var Book = sequelize.define('Book', {
title: DataTypes.STRING,
summary: DataTypes.STRING,
isbn: DataTypes.STRING,
url: DataTypes.STRING
}, {});
Book.associate = function(models) {
// associations can be defined here
Book.belongsTo(models.Author, {
onDelete: 'CASCADE',
foreignKey: {
allowNull: false
}
});
Book.hasMany(models.BookInstance);
Book.belongsToMany(models.Genre, {
through: 'BookGenres',
onDelete: 'CASCADE'
});
};
return Book;
};
【问题讨论】:
-
我收到过很多反对票 :) 我没有足够的续集经验。抱歉我的问题不好。
标签: node.js express sequelize.js