如何在Node.js中修复“无法发布错误”?

huangapple go评论64阅读模式
英文:

how do I fix 'cannot post error' in nodejs?

问题

Method: post

Url: localhost:8080/api/v1/users/:userid/subscribe/:businessid

AC: 当前用户的关注数组中添加商家id。

将当前用户id添加到商家的关注者数组中。

最终返回状态码为201,{user}

但每次我在Postman中尝试测试它时,都会收到以下错误:

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Error</title>
</head>
<body>
<pre>Cannot POST /api/v1/users/647965651bbc93663ec695d5/subscribe/647965651bbc93663ec695d6%0A</pre>
</body>
</html>

我不确定哪里出错了。

我的用户模型:

import { Request, Response } from 'express';
import UserModel from '../models/User';
import mongoose from 'mongoose';

export const subscribe = async (req: Request, res: Response) => {
  const { userid, businessid } = req.params;
  
  // 搜索用户id
  const user = await UserModel.findById(userid).exec();
  // 搜索商家id
  const business = await UserModel.findById(businessid).exec();

  if(!user || !business){
    res.status(404).json({ error: '找不到用户或商家' });
    return;
  }

  if (!user.is_business) {
    return res.status(403).json({ message: '用户不是商家' });
  }

  business.follower.addToSet(userid);
  await business.save();

  user.following.addToSet(businessid);
  await user.save();
  
  return res.status(201).json({ user });
};

用户模型:

import { Schema, model } from 'mongoose';
const schema: Schema = new Schema({

  is_business: {
    type: Boolean,
    required: true,
    default: false,
  },

  following:  [{
    type: Schema.Types.ObjectId,
    ref: "User",
  }],

  follower: [{
    type: Schema.Types.ObjectId,
    ref: "User",
  }],
},

{
  timestamps: {
    createdAt: 'created_at',
    updatedAt: 'updated_at',
  }
});
export default model('User', schema);

我的用户路由:

import { Router } from 'express';
import { subscribe } from '../../controllers/users';

const userRouter = Router();

userRouter.post('/users/subscribe/:userid/:businessid', subscribe);

export default userRouter;

有谁知道如何修复它?感谢任何帮助!

英文:

new to node.js
I'm working on a nodejs-experss-mongodb project and I'm currently implementing a subscription function with the following specific requirements:

Method: post

Url:localhost:8080/api/v1/users/:userid/subscribe/:businessid

AC:
Current user's following array, add business id.

Add the current user id to the merchant's follower array

The final return is 201, {user}

But every time I try to test it in postman, I get this error:

&lt;!DOCTYPE html&gt;
&lt;html lang=&quot;en&quot;&gt;
&lt;head&gt;
&lt;meta charset=&quot;utf-8&quot;&gt;
&lt;title&gt;Error&lt;/title&gt;
&lt;/head&gt;
&lt;body&gt;
&lt;pre&gt;Cannot POST /api/v1/users/647965651bbc93663ec695d5/subscribe/647965651bbc93663ec695d6%0A&lt;/pre&gt;
&lt;/body&gt;
&lt;/html&gt;

I'm not sure where it went wrong.
My usermodels:

import { Request, Response } from &#39;express&#39;;
import UserModel from &#39;../models/User&#39;;
import mongoose from &#39;mongoose&#39;;

export const subscribe = async (req: Request, res: Response) =&gt; {
  const { userid, businessid } = req.params;
  
  // search for student id
  const user = await UserModel.findById(userid).exec();
  // search for business id
  const business = await UserModel.findById(businessid).exec();

  if(!user || !business){
    res.status(404).json({ error: &#39;user or business not found&#39; });
    return;
  }

  if (!user.is_business) {
    return res.status(403).json({ message: &#39;User is not a business&#39; });
  }

  business.follower.addToSet(userid);
  await business.save();

  user.following.addToSet(businessid);
  await user.save();
  
  return res.status(201).json({ user });
};

usermodels:

import { Schema, model } from &#39;mongoose&#39;;
const schema: Schema = new Schema({

  is_business: {
    type: Boolean,
    required: true,
    default: false,
  },

  following:  [{
    type: Schema.Types.ObjectId,
    ref: &quot;User&quot;,
  },],

  follower: [{
    type: Schema.Types.ObjectId,
    ref: &quot;User&quot;,
  }],
},

{
  timestamps: {
    createdAt: &#39;created_at&#39;,
    updatedAt: &#39;updated_at&#39;,
  }
});
export default model(&#39;User&#39;, schema);

my user routes:

import { Router } from &#39;express&#39;;
import { index, deactivate, activate, subscribe } from &#39;../../controllers/users&#39;;

const userRouter = Router();

userRouter.post(&#39;/users/subscribe/:userid/:businessid&#39;, subscribe);

export default userRouter;

Anyone knows how to fix it? Any helps are appreciated!

答案1

得分: 1

你的路由定义如下:

'/users/subscribe/:userid/:businessid'

但是,你的URL被发送为:

'/users/:userid/subscribe/:businessid'

这两者不匹配。请更改其中一个以使其与另一个匹配。

英文:

Your route is defined as:

&#39;/users/subscribe/:userid/:businessid&#39;

But, your URL is sent as:

&#39;/users/:userid/subscribe/:businessid&#39;

These do not match. Change one of them to match the other.

答案2

得分: 0

请检查您正在重定向到的URL,我认为您已经编写了以下内容:

  • /api/v1/users/647965651bbc93663ec695d5/subscribe/647965651bbc93663ec695d6%0A

请重定向到以下内容:

/api/v1/users/(subscribe_id)/647965651bbc93663ec695d5/:businessid

英文:

Please check the URL which are you redirecting to I think
you have written these

  • /api/v1/users/647965651bbc93663ec695d5/subscribe/647965651bbc93663ec695d6%0A

  • please redirect into these
    /api/v1/users/(subscribe_id)/647965651bbc93663ec695d5/:businessid

huangapple
  • 本文由 发表于 2023年6月12日 12:11:51
  • 转载请务必保留本文链接:https://go.coder-hub.com/76453607.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定