小编Mr.*_* B.的帖子

Mongoose:remove()为已删除的项返回true

即使用户已被删除,我的代码也会返回"用户已删除".在这种情况下,我宁愿抛出404,但我想尽可能少查询数据库.

有没有办法获得userNotFound(见下文)而无需手动检查用户是否在删除前存在?也许我错过了一个功能remove()或替代功能.

var itemId = 123;
Item.remove({id: itemId}, function(err) {
    if (err) {
        return res.json({success: false, msg: 'Cannot remove item'});
    }

    // !!!
    if (userNotFound) {
        return res.status(404).json({success: false, msg: 'User not found'});
    }  
    // /!!!


    res.json({success: true, msg: 'User deleted.'});
});
Run Code Online (Sandbox Code Playgroud)

提前致谢!

database mongoose mongodb node.js express

6
推荐指数
1
解决办法
5268
查看次数

JavaScript:按密钥对项目进行分组,按其他密钥对(!)组内的项目进行排序 - 如何?

在我的结果中,学生应按Room-Id分组(!),然后在组内按年龄分类.

按房间分组/排序的结果:

Name      | R | Age  
-------------------
Student 2 | 1 | 22  
Student 4 | 1 | 25  
Student 3 | 3 | 21  
Student 6 | 3 | 27  
Student 1 | 5 | 29 <- Wrong, should be: Student 5, Room 5, Age 23 
Student 5 | 5 | 23 <- Wrong, should be: Student 1, Room 5, Age 29
Run Code Online (Sandbox Code Playgroud)

目前的代码(JSFiddle:http://jsfiddle.net/n9KNx/):

<script type="text/javascript">
/* add students */
var students = …
Run Code Online (Sandbox Code Playgroud)

javascript sorting jquery grouping

5
推荐指数
2
解决办法
5001
查看次数

MySQL最佳实践:尽可能选择子项递归?

我想选择一个根项目,它的子项尽可能高.我更喜欢使用嵌套集模型,但这次表结构遵循邻接模型.有关嵌套集和邻接模型的更多信息.

我有一个dependencies-table,一个items-table.

依赖表

dependency_id | item_id | child_id 
            1 |       1 |        4
            2 |       2 |        5
            3 |       4 |        7
            4 |       7 |        3
            5 |       9 |        3
            6 |       1 |        2
Run Code Online (Sandbox Code Playgroud)

物品表

item_id | name   | info
      1 | Item A | 1st Item
      2 | Item D | 2nd Item
      3 | Item C | 3rd Item
      4 | Item D | 4th Item …
Run Code Online (Sandbox Code Playgroud)

php mysql sql recursion

5
推荐指数
1
解决办法
3083
查看次数

Symfony 2:如何在每个控制器动作之前触发代码?

我想检查,如果用户登录-之前动作被调用.

preDispatch()for的东西Symfony\Bundle\FrameworkBundle\Controller\Controller会很棒.

我想避免这个:

class MyBaseController extends Controller {
    public function __construct() {
        // ...
        // check: logged in?
        // ...
    }
}

class MyFooController extends MyBaseController() {
    // ...
}
Run Code Online (Sandbox Code Playgroud)

我刚刚从2011/2012找到了主题.我是Symfony的新手,使用2.5版,想从一开始就知道最佳实践.有什么建议吗?

提前致谢!

编辑:

有趣:http: //symfony.com/doc/current/cookbook/event_dispatcher/before_after_filters.html

php hook event-handling symfony

5
推荐指数
1
解决办法
6885
查看次数

使用AWS的elasticsearch.js

我正在使用AWS Elasticsearch服务,并希望通过elasticsearch.js进行连接,但需要端口.

看起来AWS只提供REST API(例如通过卷曲),在端口80上运行.我的群集已启动,我可以通过浏览器访问,但不能通过elasticsearch.js访问.

这个例子对我不起作用:

var elasticsearch = require('elasticsearch');
var client = new elasticsearch.Client({
  host: 'localhost:9200', // not working: '', 80, default: 443
  log: 'trace'
});

client.ping({
  requestTimeout: 1000
}, function (error) {
  if (error) {
    console.trace('elasticsearch cluster is down!');
  } else {
    console.log('All is well');
  }
});
Run Code Online (Sandbox Code Playgroud)

我找到了http-aws-es,但它也没有用.

任何的想法?提前致谢!

javascript amazon-web-services node.js elasticsearch

5
推荐指数
2
解决办法
1249
查看次数

Linux (zip):如何找到所有不可读的文件?

我只是尝试通过以下命令压缩文件夹及其内容:

zip -r ./mytarget.zip ./mysource
Run Code Online (Sandbox Code Playgroud)

最后我收到警告

zip warning: Not all files were readable
files/entries read:  141595 (4.2G bytes)  skipped:  57 (1.8M bytes)
Run Code Online (Sandbox Code Playgroud)

我想知道跳过了哪些文件

如何找到所有不可读的文件?我没有sudo那个服务器的权限。

提前致谢!

linux terminal zip admin server

5
推荐指数
2
解决办法
4389
查看次数

Webpack和SCSS:如何使用图像路径?

我正在尝试建立一个基本的webpack项目。除了生成的CSS中生成的图像路径外,其他所有内容都很平滑。

资料夹结构

src/
    assets/
        images/
    js/
    scss/
dist/              
    assets/           <--- generated correctly, incl. images
        images/
    bundle.js
    main.bundle.css   <--- includes "wrong" paths, starting with dist/
index.htm
webpack.config.js
Run Code Online (Sandbox Code Playgroud)

webpack.config.js

var ExtractTextPlugin = require('extract-text-webpack-plugin');

module.exports = {
    watch: true,
    entry: ['./src/js/main.js', './src/scss/main.scss'],
    output: {
        filename: 'dist/bundle.js'
    },
    module: {
        rules: [
            { // regular css files
                test: /\.css$/,
                loader: ExtractTextPlugin.extract({
                    use: 'css-loader?importLoaders=1',
                }),
            },
            { // sass / scss loader for webpack
                test: /\.(sass|scss)$/,
                use: ExtractTextPlugin.extract(['css-loader', 'sass-loader'])
            },
            { // …
Run Code Online (Sandbox Code Playgroud)

css sass webpack

5
推荐指数
1
解决办法
5658
查看次数

Python marshmallow:如何允许多个模式到一个模式的字段?

我想声明一个架构字段来接受不同的架构类型,但不是任何.

Marshmallow 可能会出现类似以下情况吗?

class SchemaA(Schema):
    name = String()

class SchemaB(Schema):
    name = String()
    age = Integer()

class SchemaC(Schema)
    one_of_many_but_not_any = [SchemaA(), SchemaB(), String()]  # <- !
Run Code Online (Sandbox Code Playgroud)

python polymorphism serialization marshmallow

5
推荐指数
1
解决办法
5222
查看次数

setup.py - 如何将子文件夹设置为主包目录?

我的项目结构:

/myproject/       <- I would like to skip that folder
  /mypackage/
    /subpackage/
      mymodule.py
    run.py
- setup.py
Run Code Online (Sandbox Code Playgroud)

run.py我想从mymodule.py这样的地方导入:

from mypackage.subpackage.mymodule import something
Run Code Online (Sandbox Code Playgroud)

代替:

from myproject.mypackage.subpackage.mymodule import something
Run Code Online (Sandbox Code Playgroud)

有没有办法将setup()入口点定义为beingmypackage并skip myproject

python pip setuptools

5
推荐指数
1
解决办法
3079
查看次数

React &amp; RTK:如何从中间件调度?

我正在使用 RTK (Redux Toolkit) 和 RTK 查询来管理我的商店和 API 请求。

为了捕获全局错误,我遵循了这个例子。这工作正常,但现在我想在请求因错误而被拒绝时注销用户401 - Unauthorized

const rtkQueryErrorLogger = api => next => action => {
  if (isRejectedWithValue(action)) {
    if (action.payload.status === 401) {
      // Reset the store (not working)
      const dispatch = useDispatch();
      const dipatch(logout());
  
      // Forward to login-screen (not working)
      const navigation = useNavigation();
      navigation.push('Login');
    }
  }
  return next(action);
};
Run Code Online (Sandbox Code Playgroud)

任何想法?提前致谢!

reactjs react-native redux react-redux redux-toolkit

5
推荐指数
1
解决办法
6375
查看次数