我在SQL Server数据库中有这个表:表名是 small_customer :
customerid| first_name | last_name | starting_date |
--------- | -----------| --------- | --------------|
这是 CustomerModel.js 中的代码,我连接到SQL Server数据库和Sequelize类型:
const CustomerModel= Conn.define('small_customer',{
customerid: {type: Sequelize.INTEGER},
first_name: {type: Sequelize.STRING},
last_name: {type: Sequelize.STRING},
starting_date: {type:Sequelize.DATEONLY},
});
const Customer= Conn.models.small_customers;
export default Customer;
这是 Query.js :
import { CustomerModel, CustomerType, CustomerArgs} from './models/customer';
export default new GraphQLObjectType({
name: 'Query',
fields: () => {
return {
customer : {
type: CustomerType,
args: CustomerArgs,
resolve(root,args){
return new Promise((resolve, reject) => {
return resolve(Customer.find({ where: args }));
}); } }}}});
我已定义 CustomerType.js :
export default new GraphQLObjectType({
name: 'Customer',
fields: () => {
return {
customerid: {type:GraphQLInt},
first_name: {type: GraphQLString},
last_name : {type: GraphQLString},
starting_date: {type: GraphQLDate},
}}});
当我编写查询以便根据customerid和firstname选择客户数据时,我有这个错误
它无法读取未定义的属性'find'
这是我的查询示例和GraphiQL中的结果:
答案 0 :(得分:1)
Cannot read property 'find' of undefined
表示客户具有undefined
类型,并且您正在find
上执行undefined
操作Customer.find({ where: args })
在CustomerModel
Query.js
import { Customer } from './models/CustomerModel';
// i haven't used babel so require, if only schema is defined in CustomerModel
// const Customer = require('./models/CustomerModel');
import { CustomerModel, CustomerType, CustomerArgs } from './models/customer';
export default new GraphQLObjectType({
name: 'Query',
fields: () => {
return {
customer: {
type: CustomerType,
args: CustomerArgs,
resolve(root, args) {
return new Promise((resolve, reject) => {
return resolve(Customer.find({ where: args }));
});
}
}
}
}
});