求助,sequelize+express多表查询提示TypeError: person.getDeperson is not a function
发布于 1 个月前 作者 Remix123 267 次浏览 来自 问答

模型定义为

//Depart
'use strict';
module.exports = function (sequelize, DataTypes) {
	return sequelize.define('Depart', {
		id:{type:DataTypes.BIGINT(11), autoIncrement:true, primaryKey : true, unique : true},
		departname: { type: DataTypes.STRING,  allowNull: false, }
	},
	{
		freezeTableName: true,
		tableName: 'depart',
	});
}
\n``` 
```js\n
//Person
'use strict';
module.exports = function (sequelize, DataTypes) {
	return sequelize.define('Person', {
		id:{type:DataTypes.BIGINT(11), autoIncrement:true, primaryKey : true, unique : true},
		name: { type: DataTypes.STRING,  allowNull: false },
        departId:{type: DataTypes.BIGINT(11), field: 'depart_id', allowNull: false }
	},
	{
		freezeTableName: true,
        tableName: 'person',  
        indexes: [{
            name: 'person_departId',
            method: 'BTREE',
            fields: ['depart_id']
        }]
	});
}
\n```
模型关系定义为
```js\n
'use strict';
var sequelize = require('./db').sequelize();
var Depart = sequelize.import('./depart.js');
var Person = sequelize.import('./person.js');
Depart.hasMany(Person, { foreignKey: 'depart_id', targetKey: 'id', as: 'Deperson' });
Person.belongsTo(Depart, { foreignKey: 'depart_id', targetKey: 'id' });
sequelize.sync();
exports.Depart = Depart;
exports.Person = Person;
\n``` 
查询接口定义为
```js\n
var express = require('express');
var router = express.Router();
var Person = require('../models').Person;
var Depart = require('../models').Depart;
router.get('/query', function (req, res, next) {
    Person.findOne().then(function (person) {
        person.getDeperson();
        res.set('Content-Type', 'text/html; charset=utf-8');
        res.end(JSON.stringify(person));
    }).catch(next);
});
\n``` 
访问/query接口报错
```js\n
TypeError: person.getDeperson is not a function
at Model.<anonymous> (D:\VueProject\manage-system\service\controls\personApi.js:21:16)
    at Model.tryCatcher (D:\VueProject\manage-system\node_modules\[email protected]@bluebird\js\release\util.js:16:23)
    at Promise._settlePromiseFromHandler (D:\VueProject\manage-system\node_modules\[email protected]@bluebird\js\release\promise.js:512:31)
    at Promise._settlePromise (D:\VueProject\manage-system\node_modules\[email protected]@bluebird\js\release\promise.js:569:18)
    at Promise._settlePromise0 (D:\VueProject\manage-system\node_modules\[email protected]@bluebird\js\release\promise.js:614:10)
    at Promise._settlePromises (D:\VueProject\manage-system\node_modules\[email protected]@bluebird\js\release\promise.js:693:18)
    at Async._drainQueue (D:\VueProject\manage-system\node_modules\[email protected]@bluebird\js\release\async.js:133:16)
    at Async._drainQueues (D:\VueProject\manage-system\node_modules\[email protected]@bluebird\js\release\async.js:143:10)
    at Immediate.Async.drainQueues (D:\VueProject\manage-system\node_modules\[email protected]@bluebird\js\release\async.js:17:14)
    at runCallback (timers.js:672:20)
    at tryOnImmediate (timers.js:645:5)
    at processImmediate [as _immediateCallback] (timers.js:617:5)
	\n```
	
	**把  person.getDeperson();  这段去掉就正常了,但是去掉了就不能联查了,在另一个Demo里这么写就正常,请教各位大神这是哪里有问题呢**
3 回复

person.getDepart() 试试

@a304885433 感谢,改了之后变成TypeError: person.getDepart is not a function,不知道是哪里的问题,看了几遍文档也没有

按照你的代码我试了一下 models/depart.js

'use strict';
module.exports = function (sequelize, DataTypes) {
	return sequelize.define('Depart', {
		id:{type:DataTypes.BIGINT(11), autoIncrement:true, primaryKey : true, unique : true},
		departname: { type: DataTypes.STRING,  allowNull: false, }
	},
	{
		freezeTableName: true,
		tableName: 'depart',
	});
}

models/person.js

'use strict';
module.exports = function (sequelize, DataTypes) {
	return sequelize.define('Person', {
		id:{type:DataTypes.BIGINT(11), autoIncrement:true, primaryKey : true, unique : true},
		name: { type: DataTypes.STRING,  allowNull: false },
        departId:{type: DataTypes.BIGINT(11), field: 'depart_id', allowNull: false }
	},
	{
		freezeTableName: true,
        tableName: 'person',  
        indexes: [{
            name: 'person_departId',
            method: 'BTREE',
            fields: ['depart_id']
        }]
	});
}

modes\index.js

var Sequelize = require('sequelize');

var sequelize = new Sequelize('egg', 'root', 'root', {
  host: 'localhost',
  dialect: 'mysql',

  pool: {
    max: 5,
    min: 0,
    idle: 10000
  },
});
var Depart = sequelize.import('./depart.js');
var Person = sequelize.import('./person.js');
Depart.hasMany(Person, { foreignKey: 'depart_id', targetKey: 'id'});
Person.belongsTo(Depart, { foreignKey: 'depart_id', targetKey: 'id' });
sequelize.sync();
exports.Depart = Depart;
exports.Person = Person;

index.js

var express = require('express');
var Person = require('./models').Person;
var Depart = require('./models').Depart;

var app = express();

app.get('/', function (req, res,next) {
   Person.findOne().then(function (person) {
        person.getDepart();
        res.set('Content-Type', 'text/html; charset=utf-8');
        res.end(JSON.stringify(person));
    }).catch(next);
});
 app.listen(3000);
 
 最后页面输出:

这是localhost:3000的输出
{"id":2,"name":"111","departId":1,"createdAt":"2018-01-01T00:00:00.000Z","updatedAt":"2018-01-01T00:00:00.000Z","depart_id":1}

最后多尝试下,代码是没有问题的,如果遇到问题可以在联系我。

untitled1.pngQQ20170526-0@2x.png

回到顶部