[英]Koa/Node.js - Use multiple models within one controller function
I am attempting to create a new 'Vault' from a Post Request from my frontend (built in React) using Mongoose Schema. 我试图使用Mongoose Schema从前端(内置于React中)的发布请求中创建一个新的“ Vault”。
When I hit the create button in my app, the Post Request initiates, but returns: 当我在应用程序中点击“创建”按钮时,“发布请求”会启动,但会返回:
POST http://localhost:3000/vaults/create 500 (Internal Server Error)
When my controller function createVault() is initiated, it will successfully create a 'Vault' from the model (see below): 当我的控制器函数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;
}
}
}
However, my problem occurs when I try to add a second Schema model; 但是,当我尝试添加第二个Schema模型时,会出现我的问题。 I am attempting to create a 'Crypt' from each of the items in the ctx.request.body.crypts array (see below):
我正在尝试从ctx.request.body.crypts数组中的每个项目创建一个“地穴”(请参见下文):
//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;
}
}
};
The error I receive say I cannot map over an undefined object, although I am using const crypts = await Promise.all(promises);
我收到的错误消息说,尽管我使用的是
const crypts = await Promise.all(promises);
,但我无法映射未定义的对象const crypts = await Promise.all(promises);
Can anyone suggest a correct way around this problem? 谁能提出解决此问题的正确方法? Many thanks.
非常感谢。
I managed to fix my problem by creating a function called cleanBody(body) which manually parsed the data for me to use. 我设法通过创建一个称为cleanBody(body)的函数来解决我的问题,该函数手动解析数据供我使用。
I logged typeof ctx.request.body
which returned a string and revealed my problem. 我记录了
typeof ctx.request.body
,它返回了一个字符串并显示了我的问题。 The cleanBody(body) function just check if the body was an object, then used JSON.parse() if it was a string (see below): cleanBody(body)函数只是检查body是否是一个对象,然后使用JSON.parse()如果它是一个字符串(请参见下文):
const cleanBody = body => {
return typeof body !== 'object' ? JSON.parse(body) : body;
};
My mistake was assuming APIs from Postman and APIs called in the app would pass the same data, even when everything looks the same 我的错误是假设Postman的API和应用程序中调用的API会传递相同的数据,即使一切看起来相同
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.