Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/reactjs/25.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/mongodb/11.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/vue.js/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Reactjs Next.js API路由_Reactjs_Mongodb_Mongoose_Next.js - Fatal编程技术网

Reactjs Next.js API路由

Reactjs Next.js API路由,reactjs,mongodb,mongoose,next.js,Reactjs,Mongodb,Mongoose,Next.js,希望从熟悉Next.js的人那里得到一些帮助。我在将我的express API路由转换为页面中的Next.js内部API路由时遇到了问题,这看起来非常有希望。问题是它似乎不适用于我的猫鼬模型和方法 例如: 不起作用: 作品: 不起作用: 不确定我是否只是做错了,或者我的某些设置不正确。希望得到一些帮助。 我的用户模型供参考: const mongoose = require('mongoose') const UserSchema = new mongoose.Schema({ fullN

希望从熟悉Next.js的人那里得到一些帮助。我在将我的express API路由转换为页面中的Next.js内部API路由时遇到了问题,这看起来非常有希望。问题是它似乎不适用于我的猫鼬模型和方法

例如:

不起作用:

作品:

不起作用:

不确定我是否只是做错了,或者我的某些设置不正确。希望得到一些帮助。 我的用户模型供参考:

const mongoose = require('mongoose')

const UserSchema = new mongoose.Schema({
  fullName: {
    type: String,
    required: true,
  },
  email: {
    type: String,
    required: true,
  },
  userName: {
    type: String,
    required: true,
  },
  password: {
    type: String,
    required: true,
  },
  posts: {
    type: [{ type: mongoose.Schema.Types.ObjectId, ref: 'foodPost' }],
  },
  saves: {
    type: [{ type: mongoose.Schema.Types.ObjectId, ref: 'foodPost' }],
  },
  photo: {
    type: String,
    default: 'https://via.placeholder.com/400',
  },
  followingCount: {
    type: Number,
    default: 0,
  },
  followerCount: {
    type: Number,
    default: 0,
  },
  following: {
    type: Array,
    default: [],
  },
  followers: {
    type: Array,
    default: [],
  },
  startDate: {
    type: Date,
    default: Date.now(),
  },
  notifications: {
    type: Array,
    default: [],
  },
})
export default mongoose.models.user || mongoose.model('user', UserSchema)
必须更改导出,以便停止提供覆盖错误


谢谢大家!

试试这样做。。。这篇文章有点长,但如果你遵循逻辑和导入顺序,它应该可以工作:

项目结构示例:

├── .next
|
├── database
|   └── index.js
|
├── models
|   ├── all.js
|   ├── index.js
|   └── teams.js
|
├── src
|   └── pages
|       ├── api
|       |   └── teams
|       |       └── names.js
|       └── index.js
|
└── next.config.json
database/index.js-这将使用mongoose建立到mongo数据库的连接

const蓝鸟=requirebluebird; const mongoose=所需的mongoose; //我使用process.env.DATABASE作为灵活的数据库名称 //这可以在.env文件或package.json脚本中设置。。。 //比如dev:DATABASE=example-dev-next-dev const{DATABASE}=process.env; 常量选项={ useNewUrlParser:true,//避免弃用警告:当前URL字符串解析器已弃用 useCreateIndex:true,//避免弃用警告:collection.ensureIndex已弃用。 UseFindModify:false,//避免弃用警告:collection.FindModify已弃用。 useUnifiedTopology:true,//避免弃用警告:当前服务器发现和监视引擎已弃用 }; //连接到mongodb数据库 猫鼬连接`mongodb://localhost/${DATABASE}`,选项; //用蓝鸟做猫鼬 猫鼬。承诺=蓝鸟; models/all.js-这将要求所有模型都注册到数据库连接

models/index.js-这将导出mongoose模型实例,您不需要此文件,因为您可以只导入mongoose并从文件中检索modelTeam,但我喜欢减少重复导入

const{model}=requiremongose; module.exports={ 团队:模型团队, 用户:modelUser, 等 }; models/team.js-这将把模式建立为mongoose模型

const{Schema,model}=requiremongose; const teamSchema=新模式{ 联盟:{type:String,必需:true}, 团队:{type:String,unique:true}, 名称:{type:String,unique:true,小写:true}, }; module.exports=modelTeam,teamSchema; pages/api/teams/all.js-从api路由中导入模型实例

从../../../models导入{Team};index.js //或者: //从猫鼬中导入{model}; //成本团队=模型团队; /** *检索所有团队名称。 * *@function getAllTeamNames *@返回{object}-名称 *@throws{string} */ const getAllTeamNames=async\uRes=>{ //将所有团队名称聚合到单个数组中,如下所示: //[{名称:[团队1、团队2、团队3、团队4等]] const teams=等待Team.aggregate[ {$group:{{u id:null,名称:{$addToSet:$team}}, {$unwind:$NAME}, {$sort:{names:1}}, {$group:{{u id:null,name:{$push:$names}}, {$project:{{u id:0,名称:1}}, ]; //返回名称数组[team1、team2、team3、team4等] json{names:teams[0].names}; }; 导出默认getAllTeamNames; next.config.js-这将在下次加载之前建立连接池并注册模型

需要./database;//建立mongo连接 要求./models/all;//将模型注册到连接 等
试着这样做。。。这篇文章有点长,但如果你遵循逻辑和导入顺序,它应该可以工作:

项目结构示例:

├── .next
|
├── database
|   └── index.js
|
├── models
|   ├── all.js
|   ├── index.js
|   └── teams.js
|
├── src
|   └── pages
|       ├── api
|       |   └── teams
|       |       └── names.js
|       └── index.js
|
└── next.config.json
database/index.js-这将使用mongoose建立到mongo数据库的连接

const蓝鸟=requirebluebird; const mongoose=所需的mongoose; //我使用process.env.DATABASE作为灵活的数据库名称 //这可以在.env文件或package.json脚本中设置。。。 //比如dev:DATABASE=example-dev-next-dev const{DATABASE}=process.env; 常量选项={ useNewUrlParser:true,//避免弃用警告:当前URL字符串解析器已弃用 useCreateIndex:true,//避免弃用警告:collection.ensureIndex已弃用。 UseFindModify:false,//避免弃用警告:collection.FindModify已弃用。 useUnifiedTopology:true,//避免弃用警告:当前服务器发现和监视引擎已弃用 }; //连接到mongodb数据库 猫鼬连接`mongodb://localhost/${DATABASE}`,选项; //用蓝鸟做猫鼬 猫鼬。承诺=蓝鸟; models/all.js-这将要求所有模型都注册到数据库连接

models/index.js-这将导出您不需要此文件的mongoose模型实例,因为您只需导入mongoose并检索modelTeam即可 从一个文件中,但我喜欢减少重复导入

const{model}=requiremongose; module.exports={ 团队:模型团队, 用户:modelUser, 等 }; models/team.js-这将把模式建立为mongoose模型

const{Schema,model}=requiremongose; const teamSchema=新模式{ 联盟:{type:String,必需:true}, 团队:{type:String,unique:true}, 名称:{type:String,unique:true,小写:true}, }; module.exports=modelTeam,teamSchema; pages/api/teams/all.js-从api路由中导入模型实例

从../../../models导入{Team};index.js //或者: //从猫鼬中导入{model}; //成本团队=模型团队; /** *检索所有团队名称。 * *@function getAllTeamNames *@返回{object}-名称 *@throws{string} */ const getAllTeamNames=async\uRes=>{ //将所有团队名称聚合到单个数组中,如下所示: //[{名称:[团队1、团队2、团队3、团队4等]] const teams=等待Team.aggregate[ {$group:{{u id:null,名称:{$addToSet:$team}}, {$unwind:$NAME}, {$sort:{names:1}}, {$group:{{u id:null,name:{$push:$names}}, {$project:{{u id:0,名称:1}}, ]; //返回名称数组[team1、team2、team3、team4等] json{names:teams[0].names}; }; 导出默认getAllTeamNames; next.config.js-这将在下次加载之前建立连接池并注册模型

需要./database;//建立mongo连接 要求./models/all;//将模型注册到连接 等
不幸的是,在过去的一天里,我尝试了这个方法,但没有成功。不过还是要谢谢你!这是一个有效的回购协议:它有点过时,因为我没有时间更新它,但它仍然有效。谢谢!我将尝试在回购协议中更多地使用它。在撕开我的代码并通过你的回购协议后,我让它工作了!非常感谢你的帮助,马特。疯狂的你要做多少才能让它和下一个js玩得很好。很高兴我能帮上忙。当处理SSR时,没有什么是容易的。不幸的是,在过去一天尝试了这个之后,我无法让它工作。不过还是要谢谢你!这是一个有效的回购协议:它有点过时,因为我没有时间更新它,但它仍然有效。谢谢!我将尝试在回购协议中更多地使用它。在撕开我的代码并通过你的回购协议后,我让它工作了!非常感谢你的帮助,马特。疯狂的你要做多少才能让它和下一个js玩得很好。很高兴我能帮上忙。在处理SSR时,没有什么是容易的。
const mongoose = require('mongoose')

const UserSchema = new mongoose.Schema({
  fullName: {
    type: String,
    required: true,
  },
  email: {
    type: String,
    required: true,
  },
  userName: {
    type: String,
    required: true,
  },
  password: {
    type: String,
    required: true,
  },
  posts: {
    type: [{ type: mongoose.Schema.Types.ObjectId, ref: 'foodPost' }],
  },
  saves: {
    type: [{ type: mongoose.Schema.Types.ObjectId, ref: 'foodPost' }],
  },
  photo: {
    type: String,
    default: 'https://via.placeholder.com/400',
  },
  followingCount: {
    type: Number,
    default: 0,
  },
  followerCount: {
    type: Number,
    default: 0,
  },
  following: {
    type: Array,
    default: [],
  },
  followers: {
    type: Array,
    default: [],
  },
  startDate: {
    type: Date,
    default: Date.now(),
  },
  notifications: {
    type: Array,
    default: [],
  },
})
export default mongoose.models.user || mongoose.model('user', UserSchema)
├── .next
|
├── database
|   └── index.js
|
├── models
|   ├── all.js
|   ├── index.js
|   └── teams.js
|
├── src
|   └── pages
|       ├── api
|       |   └── teams
|       |       └── names.js
|       └── index.js
|
└── next.config.json
require("./team");
require("./user");
...etc