RError.com

RError.com Logo RError.com Logo

RError.com Navigation

  • 主页

Mobile menu

Close
  • 主页
  • 系统&网络
    • 热门问题
    • 最新问题
    • 标签
  • Ubuntu
    • 热门问题
    • 最新问题
    • 标签
  • 帮助
主页 / 问题

问题[express]

Martin Hope
webtensei
Asked: 2022-09-25 16:02:33 +0000 UTC

猫鼬 findByIdAndUpdate 不保存对数据库的更改

  • 0

我的控制器中有一个更改用户数据的功能:

  async editUser(req, res) {
    try {
      const { username, password, name, surname, patronymic, userRole } =
        req.body;
      var hashPassword = bcrypt.hashSync(password, 5);
      User.findByIdAndUpdate(mongoose.Types.ObjectId(req.params.id), {
        username,
        password: hashPassword,
        name,
        surname,
        patronymic,
        roles: [userRole],
      });
      req.session.message = {
        iconStyle: "check",
        type: "check-Success",
        intro: "Успешно",
        message: "Информация обновлена.",
      };
      res.status(400).redirect("/users");
    } catch (e) {
      console.log(e);
      res.status(400).json({ message: "nt" });
    }
  }

还有路线:

router.post("/editUser/(:id)", controller.editUser);

当通过邮递员发送一个帖子请求时,数据更改功能起作用,它读取我,但用户数据没有改变。req.body 当然是完整的并且 req.params.id 属于现有用户。我只是在学习,告诉我 - 可能是什么问题?

express
  • 1 个回答
  • 14 Views
Martin Hope
Артем Аверкин
Asked: 2022-07-23 03:03:50 +0000 UTC

服务器无法启动,找不到模块

  • 0

我遇到了一个问题 - 服务器没有使用 npm run dev 命令启动。引发此错误:

Error: Cannot find module 'C:\Users\Артем\Desktop\Диплом\Web-Zen\Server\index.js'
    at Function.Module._resolveFilename (internal/modules/cjs/loader.js:889:15)
    at Function.Module._load (internal/modules/cjs/loader.js:745:27)
    at Function.executeUserEntryPoint [as runMain] (internal/modules/run_main.js:76:12)
    at internal/main/run_main_module.js:17:47 {
  code: 'MODULE_NOT_FOUND',
  requireStack: []
}
[nodemon] app crashed - waiting for file changes before starting...

index.js 文件:

const express = require('express')

const PORT = 5000

const app = express()

app.listen(PORT, () => console.log('Server started on port ${5000}'))

package.json 文件:

{
  "name": "server",
  "version": "1.0.0",
  "description": "",
  "main": "index.js",
  "scripts": {
    "dev": "nodemon index.js"
  },
  "keywords": [],
  "author": "",
  "license": "ISC",
  "dependencies": {
    "cors": "^2.8.5",
    "dotenv": "^16.0.1",
    "express": "^4.18.1",
    "pg": "^8.7.3",
    "pg-hstore": "^2.3.4",
    "sequelize": "^6.19.2"
  },
  "devDependencies": {
    "nodemon": "^2.0.16"
  }
}

不知道如何启动服务器?

node.js express
  • 1 个回答
  • 46 Views
Martin Hope
Hat
Asked: 2022-08-27 16:28:37 +0000 UTC

再次请求相同的 url 时有延迟

  • 1

此函数向我的 React Native 应用程序中的服务器发送请求。

async function request(url, method = 'GET', data, contentType = 'application/json') {
  const state = store.getState()
  const config = {
    method,
    headers: {
      'Accept': 'application/json',
      'Authorization': state.user.token
    }
  }
  if (contentType === 'application/json') config.headers['Content-Type'] = 'application/json'

  if (method === 'POST' || method === 'PATCH') {
    config.body = data
  }
  console.log(url, new Date())
  const response = await fetch(url, config)
  return await response.json()
} 

快递上的服务器。带有控制器的路由示例。

router.get('/message/:userID', passport.authenticate('jwt', {session: false}), controller.getAllMessage)
module.exports.getAllMessage = async function(req, res) {   
    try {
      console.log('getAllMessage', new Date())
      //какой-то код
      res.status(200).json(message)
      
    } catch (e) {
      errorHandler(res, e)
    }
  }

在此示例中,如果我第一次发送请求,则日志之间的时间差异很小,如果请求经过一秒钟然后到达相同的 url,或者与:userID已经存在的 url 不同的 url,则延迟是不确定的秒数,平均为 15-30 秒。其他路线也一样。在 Postman 和 Angular Web 客户端中,所有请求都不会延迟。不知道这里有什么问题。任何帮助将不胜感激。

如果您泄漏问题不在提供的代码中,您可以查看完整代码。

服务器:https ://github.com/ZRomanova/emo_new

客户端:https ://github.com/ZRomanova/emo_mobile

express
  • 1 个回答
  • 10 Views
Martin Hope
dragalur
Asked: 2022-07-19 01:05:32 +0000 UTC

更新 MongooDB 对象数组中特定对象的一个​​字段。猫鼬

  • 0

数据库包含一个achivement带有对象数组的字段()。您需要在数组中找到其字段name对应于搜索值的对象并更新该字段currentPoint。
猫鼬模式代码:

const achive = new Schema(
   {
      achiveId: ObjectId,
      name: { type: String, required: true },
      finishedPoints: { type: Number, required: true },
      currentPoints: {
         type: Number,
         default: 0,
         set: function (v) {
            if (v >= this.finishedPoints) this.isFinished = true;
            return v;
         }
      },
      isFinished: { type: Boolean, default: false }
   },
   { _id: false }
);

const achivesSchema = new Schema({
   userId: ObjectId,
   achivement: [achive]
});

更新代码:

export async function progressAchive(req, res) {
   const value = 3;
   try {
      const test = await Achives.updateOne(
         {
            userId: req.user._id,
            achivement: { $elemMatch: { name: req.params.nameAchive } }
         },
         { $set: { achivement: { currentPoints: value } } },

         { new: true }
      );
      res.json(test);
   } catch (e) {
      console.log(e);
   }
}

它不是更新,而是从数组中删除所有对象,并使用currentPoint. 你能告诉我如何更新数据库中的记录吗?

express
  • 1 个回答
  • 10 Views
Martin Hope
Suren Khachatryan
Asked: 2022-05-10 18:06:50 +0000 UTC

未捕获的 TypeError:无法解析模块说明符“express”。相对引用必须以“/”、“./”或“../”开头

  • 0

未捕获的 TypeError:无法解析模块说明符“express”。相对引用必须以“/”、“./”或“../”开头。错误突然出现(我已经尝试重新安装express等,它没有帮助。我不知道代码是否有帮助,但我附上它以防万一。请遇到这个的人帮助我快递错误。提前致谢

import express from 'express';
import path from 'path';
//--contact-form
import contact from './routes/contact.js';
//--login-form
import auth from './routes/auth.js';
//--register-form
import register from './routes/register.js';

const __dirname = path.resolve();
const port = process.env.port || 3000;
const app = express();

app.use(express.static(path.join(__dirname + '/build')));
app.use('/styles', express.static(__dirname + 'build/styles'))

app.set('view engine', 'ejs')

app
  .route('/')
  .get((req, res) => {
    res.render('index', {title: 'ОптПоставка'})
  });

app
  .route('/catalog')
  .get((req, res) => {
    res.render('catalog', {title: 'Каталог'})
  });


//-------

app.use(express.json());
app.use('/contact', contact);
app.use('/login', auth);
app.use('/register', register);


app.listen(port, () => {
  console.log(`Server running on port ${port}...`);
});

express
  • 1 个回答
  • 10 Views

Sidebar

Stats

  • 问题 10021
  • Answers 30001
  • 最佳答案 8000
  • 用户 6900
  • 常问
  • 回答
  • Marko Smith

    我看不懂措辞

    • 1 个回答
  • Marko Smith

    请求的模块“del”不提供名为“default”的导出

    • 3 个回答
  • Marko Smith

    "!+tab" 在 HTML 的 vs 代码中不起作用

    • 5 个回答
  • Marko Smith

    我正在尝试解决“猜词”的问题。Python

    • 2 个回答
  • Marko Smith

    可以使用哪些命令将当前指针移动到指定的提交而不更改工作目录中的文件?

    • 1 个回答
  • Marko Smith

    Python解析野莓

    • 1 个回答
  • Marko Smith

    问题:“警告:检查最新版本的 pip 时出错。”

    • 2 个回答
  • Marko Smith

    帮助编写一个用值填充变量的循环。解决这个问题

    • 2 个回答
  • Marko Smith

    尽管依赖数组为空,但在渲染上调用了 2 次 useEffect

    • 2 个回答
  • Marko Smith

    数据不通过 Telegram.WebApp.sendData 发送

    • 1 个回答
  • Martin Hope
    Alexandr_TT 2020年新年大赛! 2020-12-20 18:20:21 +0000 UTC
  • Martin Hope
    Alexandr_TT 圣诞树动画 2020-12-23 00:38:08 +0000 UTC
  • Martin Hope
    Air 究竟是什么标识了网站访问者? 2020-11-03 15:49:20 +0000 UTC
  • Martin Hope
    Qwertiy 号码显示 9223372036854775807 2020-07-11 18:16:49 +0000 UTC
  • Martin Hope
    user216109 如何为黑客设下陷阱,或充分击退攻击? 2020-05-10 02:22:52 +0000 UTC
  • Martin Hope
    Qwertiy 并变成3个无穷大 2020-11-06 07:15:57 +0000 UTC
  • Martin Hope
    koks_rs 什么是样板代码? 2020-10-27 15:43:19 +0000 UTC
  • Martin Hope
    Sirop4ik 向 git 提交发布的正确方法是什么? 2020-10-05 00:02:00 +0000 UTC
  • Martin Hope
    faoxis 为什么在这么多示例中函数都称为 foo? 2020-08-15 04:42:49 +0000 UTC
  • Martin Hope
    Pavel Mayorov 如何从事件或回调函数中返回值?或者至少等他们完成。 2020-08-11 16:49:28 +0000 UTC

热门标签

javascript python java php c# c++ html android jquery mysql

Explore

  • 主页
  • 问题
    • 热门问题
    • 最新问题
  • 标签
  • 帮助

Footer

RError.com

关于我们

  • 关于我们
  • 联系我们

Legal Stuff

  • Privacy Policy

帮助

© 2023 RError.com All Rights Reserve   沪ICP备12040472号-5