Quantcast
Channel: CNode:Node.js专业中文社区
Viewing all articles
Browse latest Browse all 14821

求助,sequelize+express多表查询提示TypeError: person.getDeperson is not a function

$
0
0

模型定义为

//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\_bluebird@3.5.0@bluebird\js\release\util.js:16:23)
    at Promise._settlePromiseFromHandler (D:\VueProject\manage-system\node_modules\_bluebird@3.5.0@bluebird\js\release\promise.js:512:31)
    at Promise._settlePromise (D:\VueProject\manage-system\node_modules\_bluebird@3.5.0@bluebird\js\release\promise.js:569:18)
    at Promise._settlePromise0 (D:\VueProject\manage-system\node_modules\_bluebird@3.5.0@bluebird\js\release\promise.js:614:10)
    at Promise._settlePromises (D:\VueProject\manage-system\node_modules\_bluebird@3.5.0@bluebird\js\release\promise.js:693:18)
    at Async._drainQueue (D:\VueProject\manage-system\node_modules\_bluebird@3.5.0@bluebird\js\release\async.js:133:16)
    at Async._drainQueues (D:\VueProject\manage-system\node_modules\_bluebird@3.5.0@bluebird\js\release\async.js:143:10)
    at Immediate.Async.drainQueues (D:\VueProject\manage-system\node_modules\_bluebird@3.5.0@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里这么写就正常,请教各位大神这是哪里有问题呢**

Viewing all articles
Browse latest Browse all 14821

Trending Articles