小编Den*_*Hiu的帖子

如何测试使用 Tokio 的异步函数?

我有一个需要测试的异步函数。该函数使用一个mongodb::Database对象来运行,因此我在函数中初始化连接setup()并使用tokio_test::block_on()将表达式包装await在其中。

#[cfg(test)]
mod tests {
    use mongodb::{options::ClientOptions, Client};
    use tokio_test;

    async fn setup() -> mongodb::Database {
        tokio_test::block_on(async {
            let client_uri = "mongodb://127.0.0.1:27017";
            let options = ClientOptions::parse(&client_uri).await;
            let client_result = Client::with_options(options.unwrap());
            let client = client_result.unwrap();
            client.database("my_database")
        })
    }

    #[test]
    fn test_something_async() {
        // for some reason, test cannot be async
        let DB = setup(); // <- the DB is impl std::future::Future type

        // the DB variable will be used to run another
        // …
Run Code Online (Sandbox Code Playgroud)

rust rust-tokio

77
推荐指数
1
解决办法
3万
查看次数

我需要将 Rust 读取的文件放在哪里?

当我在这里阅读 Rust 教程时(https://doc.rust-lang.org/book/ch09-02-recoverable-errors-with-result.html)。我找到了这段代码:

use std::fs::File;

fn main() {
   let f = File::open("hello.txt");

   let f = match f {
      Ok(file) => file,
      Err(error) => panic!("Problem opening the file: {:?}", error),
   };
}
Run Code Online (Sandbox Code Playgroud)

它总是显示错误:{ code: 2, kind: NotFound, message: "The system cannot find the file specified." }即使我hello.txt在 的根文件夹中创建 a src,它也无法读取它。

另一个例子中,我cargo run没有成功。程序仍然无法读取hello.txt文件。我知道该示例使用rustc open.rs && ./open. cargo run因为我不明白为什么它突然使用不同的编译方法,它是什么意思......我只是有点跳过它并尝试使用

我需要将文件放在哪里以便cargo run可以读取它?

另外,如果我运行生产代码并需要程序读取外部文件,我需要将其放在哪里?

这是我的文件夹结构。很简单,因为我刚刚开始学习 RUST。先感谢您。

文件夹结构

rust

16
推荐指数
1
解决办法
5729
查看次数

Axios.delete(url[,config]):类型与类型“AxiosRequestConfig”没有共同的属性

我使用 React、Typescript 和 Axios。我声明一个由静态函数填充的类,如下所示:

import axios from "axios"

export default class Users {

  static checkinByPassword(username: string, password: string){
    const params = {username, password}
    return axios.post(`/${this._key}/checkinbypassword`, params)
  }

  static delete(id: string){
    const params = {id: id}
    return axios.delete(`/${this._key}`, params)
  }
}
Run Code Online (Sandbox Code Playgroud)

第一个函数(checkinByPassword)工作正常。第二个函数使 ESLint(我使用 ESLint 作为 VSCode 编辑器)抛出错误:

Type '{ id: string; }' has no properties in common with type 'AxiosRequestConfig'.
Run Code Online (Sandbox Code Playgroud)

VSCode 中的屏幕截图

什么是AxiosRequestConfig?以及如何使我的 params 对象与其兼容?先感谢您

typescript reactjs eslint axios typescript-eslint

13
推荐指数
1
解决办法
1万
查看次数

针对Netbeans的Stylus CSS突出显示插件?

Stylus CSS预处理器很棒但是因为我以前在Netbeans中编码,所以没有颜色突出显示很难编码.是否有适用于Stylus的Netbeans插件?如果有的话,那将是完美的!

谢谢

html netbeans stylus netbeans-plugins

9
推荐指数
1
解决办法
1954
查看次数

如何在 Rust 中使一些 Struct 的字段强制填写,而其他字段可选?

我有一些基本结构来建模项目的单位,例如:件、盒、打等。但是我需要强制某些字段由用户定义,而有些字段则不需要。这是我default使用 Rust 文档中的构造函数实现的。我的问题是 Rust 强制在构造函数中定义所有字段:

pub struct Unit {
    pub name: String,              // this is mandatory. MUST be filled
    pub multiplier: f64,           // this is mandatory. MUST be filled
    pub price_1: Option<f64>,      // this is non-mandatory with default value NONE
    pub price_2: Option<f64>,      // this is non-mandatory with default value NONE
    pub price_3: Option<f64>,      // this is non-mandatory with default value NONE
}

// here I implement the Default just for the prices. 
// If user doesn't fill …
Run Code Online (Sandbox Code Playgroud)

rust

9
推荐指数
1
解决办法
1万
查看次数

Mongodb聚合:将日期转换为另一个时区

我保存我的交易,例如:

{code: "A", total: 250000, timestamp: ISODate("2016-01-20T23:57:05.771Z")},
{code: "B", total: 300000, timestamp: ISODate("2016-01-20T05:57:05.771Z")}
Run Code Online (Sandbox Code Playgroud)

每个事务都有timestampUTC时区下的字段.由于我住在雅加达(UTC + 7)时区,我需要在聚合之前将7小时添加到我的时间戳.这是我的mongo语法:

db.transaction.aggregate(
[
 {
   $project:
     {
       year: { $year: "$timestamp" },
       month: { $month: "$timestamp" },
       day: { $dayOfMonth: "$timestamp" }
     }
 }
])
Run Code Online (Sandbox Code Playgroud)

它返回:

    {
        "_id" : ObjectId("56a01ed143f2fd071793d63b"),
        "year" : 2016,
        "month" : 1,
        "day" : 20
    },
    {
        "_id" : ObjectId("56a01ed143f2fd071793d63b"),
        "year" : 2016,
        "month" : 1,
        "day" : 20
    }
Run Code Online (Sandbox Code Playgroud)

这是错误的,因为第一次交易(代码A)发生在1月21日,但由于它被转换为UTC(-7小时),它变成了ISODate("2016-01-20T23:57:05.771Z")

注意:我在这里也知道同样的问题,这是我到目前为止所尝试过的:

db.transaction.aggregate( …
Run Code Online (Sandbox Code Playgroud)

datetime date mongodb aggregation-framework

8
推荐指数
3
解决办法
1万
查看次数

如何使用用户名和密码使用 mongorestore/mongodump?

我知道这是一项基本的安全措施,但我无法使其与我的 mongodb 安装一起使用。每次我运行命令时:

mongorestore dump_folder -u "username" -p "password"
Run Code Online (Sandbox Code Playgroud)

它返回:

error reading database: not authorized on DATABASE_NAME to execute command { listCollections: 1, cursor: { batchSize: 0 } }
Run Code Online (Sandbox Code Playgroud)

我的问题是:我需要启用什么权限以及如何启用?

目前,我只是关闭 Mongo 的安全性,/etc/mongod.conf然后在恢复/转储我的数据库后重新启动它。

附加说明:这是我准备数据库及其用户的方式

  1. 我使用我的管理员帐户登录,创建一个名为 DATABASE001 的数据库
  2. 并创建一个用户:

    use DATABASE001
    db.createUser({ 
      user: "USER001", 
      pwd: "mypassword",
      roles:[ { role: "readWrite", db: "DATABASE001" },"dbAdmin"  ]
    })
    
    Run Code Online (Sandbox Code Playgroud)

对于角色,我添加readWrite到 DATABASE001 并且dbAdmin 我希望这个用户 (USER001) 能够备份或恢复他的数据库 (DATABASE001)。

  1. 我通过使用此命令登录对其进行了测试。它成功登录

    mongo localhost -u USER001 -p mypassword --authenticationDatabase DATABASE001
    
    Run Code Online (Sandbox Code Playgroud)
  2. 我想转储此数据库内容:

    mongodump  -u USER001 -p …
    Run Code Online (Sandbox Code Playgroud)

mongodb

8
推荐指数
1
解决办法
1万
查看次数

如何在mongodb中按月对文档进行分组?

我在 mongodb 中有一组交易数据,如下所示:

[
   {timestamp: ISODate("2015-11-10T11:33:41.075Z"), nominal: 25.121},
   {timestamp: ISODate("2015-11-22T11:33:41.075Z"), nominal: 25.121},
   {timestamp: ISODate("2015-11-23T11:33:41.075Z"), nominal: 26.121},
   {timestamp: ISODate("2015-12-03T11:33:41.075Z"), nominal: 30.121},
]
Run Code Online (Sandbox Code Playgroud)

我如何使用 mongodb 的聚合()来计算我每个月的总交易量?我试着用

db.getCollection('transaction').aggregate([
  { $group: {_id: "$timestamp", total: {$sum: "$nominal"} } }
])
Run Code Online (Sandbox Code Playgroud)

它失败了,因为我使用timestamp而不是month. 我不想为month交易数据添加另一个字段。我想为 $group 管道定制一个返回月值的函数。是否可以 ?如果是这样,如何?

mongodb mongodb-query aggregation-framework

7
推荐指数
1
解决办法
8611
查看次数

在MongoDB中限制查询结果

我的mongodb中有20,000多个文档.我刚刚得知你不能一次性查询它们.

所以我的问题是:

我想使用我的文档,find(query)然后仅限制3个文档的结果,我可以选择这些文档的起源.

例如,如果我的find()查询导致了8个文档:

[{doc1}, {doc2}, {doc3}, {doc4}, {doc5}, {doc6}, {doc7}, {doc 8}]
Run Code Online (Sandbox Code Playgroud)

命令limit(2, 3)会给出[doc3, doc4, doc5]

而且我还需要得到所有结果的总计数(没有限制),例如:length()将给出8(由find()函数产生的总文档数)

有什么建议吗?谢谢

mongodb mongodb-query aggregation-framework

7
推荐指数
2
解决办法
7646
查看次数

如何更改 Electron-Forge 默认端口?

我在他们的网站/文档中找不到有关此内容的参考。我需要将其更改为另一个端口,目前它卡在 3000。

这是我创建电子项目的方法electron-forge

yarn create electron-app my-new-app --template=typescript
Run Code Online (Sandbox Code Playgroud)

每当端口 3000 中有另一个服务时。它会抛出错误:

listen EADDRINUSE :::3000
Error: listen EADDRINUSE :::3000
    at Server.setupListenHandle [as _listen2] (net.js:1360:14)
Run Code Online (Sandbox Code Playgroud)

我的webpack.main.config.js文件:

const path = require('path');
module.exports = {
  entry: './src/index.ts',
  // Put your normal webpack config below here
  module: {
    rules: require('./webpack.rules')
  },
  resolve: {
    extensions: ['.js', '.ts', '.jsx', '.tsx', '.css', '.json']
  },
  devServer: {
    contentBase: path.join(__dirname, 'dist'),
    compress: true,
    port: 9000
  }
};
Run Code Online (Sandbox Code Playgroud)

node.js reactjs electron electron-builder electron-forge

7
推荐指数
2
解决办法
6938
查看次数