我想POST使用 next.js 在我的 api 文件夹中设置一个路由,并且我正在将数据发送到该路由,但我无法解析数据以将其实际保存在数据库中。POST在 next.js 中处理路由的最佳方法是什么。特别是解析JSON格式的数据?
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1",
"server": "nodemon backend/server.js",
"client": "npm start --prefix frontend",
"dev": "concurrently \"npm run server\" \"npm run client\""
}
Run Code Online (Sandbox Code Playgroud)
这是我在前端和后端文件夹(在根文件夹中)之外的 package.json 文件的脚本。当我运行命令时npm run dev,由于某种原因它不起作用。请注意npm run server并npm run client完美地工作,但问题是当我运行npm run dev其中包含的命令concurrently时,我不知道是什么问题,我可以做些什么来解决这个问题?这是我收到的错误消息:
npm ERR! code ELIFECYCLE
npm ERR! errno 1
npm ERR! ecommerce_shop@1.0.0 dev: `concurrently "npm run server" "npm run client"`
npm ERR! Exit status 1
npm ERR!
npm ERR! Failed at the …Run Code Online (Sandbox Code Playgroud) 我正在使用 React 并且我有多个组件在多个高阶组件中使用,我的导入有时会像这样import MyComponent from '../../../../components/MyComponent'我知道有一种更有效的方法可以通过更改 package.json 文件中的某些内容然后导入来导入它有点像这样import MyComponent from '@components/myComponent',但我不记得我该怎么做,我该怎么做?
我正在使用getStaticProps()函数,但我无缘无故地收到此错误:
错误:
.posts[0]从getStaticProps“/”中返回的序列化错误。原因:object("[object Object]") 不能序列化为 JSON。请仅返回 JSON 可序列化数据类型。
我也在使用 mongoDb 数据库,该connectDb()函数运行该mongoose.connect()函数并连接到数据库。另外,console.log()s 返回有效的 JSON 格式数据,我不知道是什么导致了这个问题,这是我的代码:
export const getStaticProps: GetStaticProps = async (
context: GetStaticPropsContext
) => {
await connectDb()
const count = await PostModel.countDocuments()
const posts = await PostModel.find()
console.log(posts)
console.log(count)
return {
props: { posts: posts, count: count },
revalidate: 10,
}
}
Run Code Online (Sandbox Code Playgroud) 我想使用passport-local-mongoose节点包将用户注册到我的mongoDB数据库,我对此没有问题,但我想要的是向数据库添加更多的用户名和密码:例如(他们的第一个和最后一个姓名、他们的角色以及几乎所有用户需要的东西)
我怎么能用passport-local-mongoose来做到这一点,或者除了那个包还有什么其他方法,我已经尝试将其他对象添加到数据库中,但由于某种原因它不起作用。这是我尝试将额外对象放入数据库的方法:
app.post('/register', function(req, res){
User.register({username: req.body.username}, req.body.password, function(err, user) {
if (err) {
console.log(err);
res.redirect('/register')
} else{
passport.authenticate('local')(req, res, function(){
User.updateOne({ username: req.body.username }, { $set: { firstName: 'firstnName', lastName: 'lastName' } })
res.redirect('/secrets')
})
};
});
})
Run Code Online (Sandbox Code Playgroud)
正如您所看到的,我使用 mongoose 的 updateOne 函数手动设置新值,但由于某种原因,我没有在数据库中看到它们。而且我在 User 模型架构中包含了名字和姓氏,这是架构:
const userSchema = new mongoose.Schema ({
email: String,
password: String,
firstName: String,
lastName: String
})
Run Code Online (Sandbox Code Playgroud)
有什么建议?
我在将图像文件上传到我的服务器时遇到问题,我在 YouTube 上观看了有关 multer 的教程,并且我做了与教程中完成的完全相同的事情,但无论出于何种原因,我都收到错误消息:(“TypeError:无法读取属性”路径'未定义”)。我用谷歌搜索错误,发现有些人有同样的问题,我试图像他们一样解决它,但它对我不起作用。
这是我的代码:
const multer = require('multer');
const storage = multer.diskStorage({
destination: function(req, file, cb) {
cb(null, './public/images/profilePictures');
},
filename: function(req, file, cb) {
cb(null, new Date().toISOString() + file.originalname);
}
});
const fileFilter = (req, file, cb) => {
// reject a file
if (file.mimetype === 'image/jpg' || file.mimetype === 'image/png') {
cb(null, true);
} else {
cb(null, false);
}
};
const upload = multer({
storage: storage,
limits: {
fileSize: 1024 * 1024 * 5
},
fileFilter: …Run Code Online (Sandbox Code Playgroud) 我正在尝试查询猫鼬模型内的项目内的对象,当我尝试使用方法find()或_.find()方法查找该对象时,由于某种原因我无法访问该对象,当console.log()我,它给了我undefined,或者当我使用array.filter()它时给了我一个空数组,这意味着我试图访问的对象不符合我在 lodash find 方法中给出的标准,但是当我查看数据库时,我看到该对象确实具有满足条件的属性。所以我不知道我做错了什么,这是我的代码:如您所见,我正在尝试获取用户单击并想要查看的项目的信息:
router.get("/:category/:itemId", (req, res) => {
console.log(req.params.itemId);
//gives the item id that the user clicked on
console.log(req.params.category);
//gives the name of category so I can find items inside it
Category.findOne({ name: req.params.category }, (err, category) => {
const items = category.items; //the array of items
console.log(items); //gives an array back
const item = _.find(items, { _id: req.params.itemId });
console.log(item); //gives the value of 'undefined' for whatever reason …Run Code Online (Sandbox Code Playgroud) 当我使用 npm 安装 npm i jquery 时发生此错误
npm ERR! Cannot read property 'extraneous' of undefined
npm ERR! A complete log of this run can be found in:
Run Code Online (Sandbox Code Playgroud)