gpt4 book ai didi

node.js - 必须使用值调用 loader.load() 函数,但得到 : undefined

转载 作者:搜寻专家 更新时间:2023-10-31 23:40:33 25 4
gpt4 key购买 nike

我正在关注这个 graphql教程,一切正常,直到我尝试使用数据加载器。

我的 server.js 是:

const start = async () => {
const mongo = await connectMongo();

const buildOptions = async req => {
const user = await authenticate(req, mongo.Users);
return {
context: {
dataloaders: buildDataloaders(mongo),
mongo,
user
},
schema
};
};
app.use('/graphql', bodyParser.json(), graphqlExpress(buildOptions));
app.use(
'/graphiql',
graphiqlExpress({
endpointURL: '/graphql',
passHeader: `'Authorization': 'bearer token-name@email.com'`
})
);
app.use('/', expressStaticGzip('dist'));
app.use('/attendance', expressStaticGzip('dist'));
app.use('/login', expressStaticGzip('dist'));

spdy.createServer(sslOptions, app).listen(process.env.PORT || 8080, error => {
if (error) {
console.error(error);
return process.exit(1);
} else {
console.info(
`App available at https://localhost:${process.env.PORT || 3000}`
);
}
});
};

我复制并粘贴 dataloaders.js:

const DataLoader = require('dataloader');

async function batchUsers(Users, keys) {
return await Users.find({ _id: { $in: keys } }).toArray();
}

module.exports = ({ Users }) => ({
userLoader: new DataLoader(keys => batchUsers(Users, keys), {
cacheKeyFn: key => key.toString()
})
});

还有我的 resolvers.js:

export default {
Query: {
allLinks: async (root, data, { mongo: { Links } }) =>
Links.find({}).toArray()
},
Mutation: {
createLink: async (root, data, { mongo: { Links }, user }) => {
const newLink = Object.assign({ postedById: user && user._id }, data);
const response = await Links.insert(newLink);
return Object.assign({ id: response.insertedIds[0] }, newLink);
},
createUser: async (root, data, { mongo: { Users } }) => {
const newUser = {
name: data.name,
email: data.authProvider.email.email,
password: data.authProvider.email.password
};
const response = await Users.insert(newUser);
return Object.assign({ id: response.insertedIds[0] }, newUser);
},
signinUser: async (root, data, { mongo: { Users } }) => {
const user = await Users.findOne({ email: data.email.email });
if (data.email.password === user.password) {
return { token: `token-${user.email}`, user };
}
}
},

Link: {
id: root => root._id || root.id,
postedBy: async ({ postedById }, data, { dataloaders: { userLoader } }) => {
return await userLoader.load(postedById);
}
},
User: {
id: root => root._id || root.id
}
};

当我尝试获取 allLinks 时出现错误:

TypeError: The loader.load() function must be called with a value,butgot: undefined.

谁能帮帮我?

最佳答案

所以我能够通过创建与用户的链接,从 Mongo 数据库中删除用户,然后查询链接的 postedBy 属性来重现错误。

我建议删除所有链接并重新创建用户(注册 + 登录),创建一个新链接,然后查询 postedBy 字段。

关于node.js - 必须使用值调用 loader.load() 函数,但得到 : undefined,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45291916/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com