Koa/Node.js - 在一个控制器函数中使用多个模型
Koa/Node.js - Use multiple models within one controller function
我正在尝试使用 Mongoose Schema 从我的前端(内置 React)的 Post 请求创建一个新的 'Vault'。
当我在我的应用程序中点击创建按钮时,Post 请求启动,但是 returns:
POST http://localhost:3000/vaults/create 500 (Internal Server Error)
当我的控制器函数 createVault() 启动时,它将成功地从模型中创建一个 'Vault'(参见下):
//Create a vault
module.exports.createVault = async (ctx, next) => {
if ('POST' != ctx.method) return await next();
try {
if (!ctx.request.body.name) {
ctx.status = 404
}
//Create new vault
const vault = await Vault.create({
name: ctx.request.body.name,
url: ctx.request.body.url,
description: ctx.request.body.description
});
await vault.save();
//Return vault
ctx.body = vault;
ctx.status = 201;
}
catch (error) {
if (error) {
console.log('Error in the createVault controller:', error);
ctx.status = error.response.status;
ctx.body = error.response.data;
}
}
}
但是,当我尝试添加第二个架构模型时出现了问题;我正在尝试从 ctx.request.body.crypts 数组中的每个项目创建一个 'Crypt'(见下文):
//Create a vault
module.exports.createVault = async (ctx, next) => {
if ('POST' != ctx.method) return await next();
try {
if (!ctx.request.body.name) {
ctx.status = 404
}
//Create new vault
const vault = await Vault.create({
name: ctx.request.body.name,
url: ctx.request.body.url,
description: ctx.request.body.description
});
//Create new crypts
const promises = await ctx.request.body.crypts.map(crypt => Crypt.create({
name: crypt
}));
//Add reference to vault
const crypts = await Promise.all(promises);
vault.crypts = crypts.map(crypt => crypt._id);
await vault.save();
//Return vault and crypts
ctx.body = [vault, crypts];
ctx.status = 201;
}
catch (error) {
if (error) {
console.log('Error in the createVault controller:', error);
ctx.status = error.response.status;
ctx.body = error.response.data;
}
}
};
我收到的错误提示我无法映射未定义的对象,尽管我使用的是 const crypts = await Promise.all(promises);
任何人都可以提出解决此问题的正确方法吗?非常感谢。
我设法通过创建一个名为 cleanBody(body) 的函数来解决我的问题,该函数手动解析数据我用。
我登录了 typeof ctx.request.body
,它返回了一个字符串并揭示了我的问题。 cleanBody(body) 函数只是检查 body 是否是 object,然后使用 JSON.parse() 如果它是一个字符串(见下文):
const cleanBody = body => {
return typeof body !== 'object' ? JSON.parse(body) : body;
};
我的错误是假设来自 Postman 的 API 和应用程序中调用的 API 会传递相同的数据,即使一切看起来都一样
我正在尝试使用 Mongoose Schema 从我的前端(内置 React)的 Post 请求创建一个新的 'Vault'。
当我在我的应用程序中点击创建按钮时,Post 请求启动,但是 returns:
POST http://localhost:3000/vaults/create 500 (Internal Server Error)
当我的控制器函数 createVault() 启动时,它将成功地从模型中创建一个 'Vault'(参见下):
//Create a vault
module.exports.createVault = async (ctx, next) => {
if ('POST' != ctx.method) return await next();
try {
if (!ctx.request.body.name) {
ctx.status = 404
}
//Create new vault
const vault = await Vault.create({
name: ctx.request.body.name,
url: ctx.request.body.url,
description: ctx.request.body.description
});
await vault.save();
//Return vault
ctx.body = vault;
ctx.status = 201;
}
catch (error) {
if (error) {
console.log('Error in the createVault controller:', error);
ctx.status = error.response.status;
ctx.body = error.response.data;
}
}
}
但是,当我尝试添加第二个架构模型时出现了问题;我正在尝试从 ctx.request.body.crypts 数组中的每个项目创建一个 'Crypt'(见下文):
//Create a vault
module.exports.createVault = async (ctx, next) => {
if ('POST' != ctx.method) return await next();
try {
if (!ctx.request.body.name) {
ctx.status = 404
}
//Create new vault
const vault = await Vault.create({
name: ctx.request.body.name,
url: ctx.request.body.url,
description: ctx.request.body.description
});
//Create new crypts
const promises = await ctx.request.body.crypts.map(crypt => Crypt.create({
name: crypt
}));
//Add reference to vault
const crypts = await Promise.all(promises);
vault.crypts = crypts.map(crypt => crypt._id);
await vault.save();
//Return vault and crypts
ctx.body = [vault, crypts];
ctx.status = 201;
}
catch (error) {
if (error) {
console.log('Error in the createVault controller:', error);
ctx.status = error.response.status;
ctx.body = error.response.data;
}
}
};
我收到的错误提示我无法映射未定义的对象,尽管我使用的是 const crypts = await Promise.all(promises);
任何人都可以提出解决此问题的正确方法吗?非常感谢。
我设法通过创建一个名为 cleanBody(body) 的函数来解决我的问题,该函数手动解析数据我用。
我登录了 typeof ctx.request.body
,它返回了一个字符串并揭示了我的问题。 cleanBody(body) 函数只是检查 body 是否是 object,然后使用 JSON.parse() 如果它是一个字符串(见下文):
const cleanBody = body => {
return typeof body !== 'object' ? JSON.parse(body) : body;
};
我的错误是假设来自 Postman 的 API 和应用程序中调用的 API 会传递相同的数据,即使一切看起来都一样