小编mar*_*ria的帖子

Mongodb更新路径会产生冲突

我试图插入新的竞争对手(如果不存在),否则仅对其进行更新,但是出现以下错误:

(node:4628) UnhandledPromiseRejectionWarning: Unhandled promise rejection (rejection id: 1): MongoError: Updating the path 'competitors' would create a conflict at 'competitors'
Run Code Online (Sandbox Code Playgroud)

这是我针对竞争对手的模型:

competitors : [
{
    userid: {type: String, default: 'x'},
    amount:  {type: Number, default: 0},
    reward:  {type: Number, default: 0},

}
Run Code Online (Sandbox Code Playgroud)

],

这是一段正在运行的代码,但是失败并再现了代码

 var newInsertMent = {userid: user._id, amount: 0};
                return Competition_global.findOneAndUpdate(
                    {"competitors.userid": user._id, _id: globalCompetition._id},
                    { $addToSet: {"competitors": newInsertMent}, $inc: {"competitors.$.amount": amount}},
                    {upsert: true,new: true}
                    ).then(function (competitionUpdate) {
                    console.log(competitionUpdate);
                    return competitionUpdate;
                });
Run Code Online (Sandbox Code Playgroud)

问:我在这里做错什么,为什么会造成冲突?

javascript mongoose mongodb node.js

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

创建一个角度解析器以用于多种用途

我正在寻找一种使用一条角度路线的方法来解决要在所有路线上使用的问题,但是要使用不同的参数:

目前,我有类似的东西:

   {
        path: 'user/:any',
        component: UserprofileComponent,
        resolve: { ProfiledUser: UserprofileResolver }
      },
Run Code Online (Sandbox Code Playgroud)

profileuser解决:

 resolve(route: ActivatedRouteSnapshot) {
    return this.GlobalService.Get('/userprofile/admin');
  }
Run Code Online (Sandbox Code Playgroud)

我实际上正在为解析器本身寻找一种使用Get函数使用的参数的方法GlobalService

我以前做了一些理论上可行的事情:

  path: 'family/panel',
        component: FamilyCoreComponent,
        canActivate: [PermissionGuardService],
        data: {roles: [0]},
Run Code Online (Sandbox Code Playgroud)

在哪里可以激活权限保护:

  canActivate(next: ActivatedRouteSnapshot, state: RouterStateSnapshot):Observable<boolean>|boolean {

    var requirementRole = next.data.roles[0];
Run Code Online (Sandbox Code Playgroud)

所以我的问题是,我应该像对权限保护员那样使用解析器的原理吗?

例如:

  {
    path: 'user/:any',
    component: UserprofileComponent,
    resolve: { ProfiledUser: UserprofileResolver }
    data: {load: 'userprofile/admin'},
  },
Run Code Online (Sandbox Code Playgroud)

这会是一个好方法吗?如果是这样,我将如何使其变得最高效?

javascript routes resolver angular

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

在res范围之外的node.js中使用i18n-2

我试图瞄准,以便我可以在所调用的函数中使用i18n。

我有错误:

(node:15696) UnhandledPromiseRejectionWarning: TypeError: i18n.__ is not a function
Run Code Online (Sandbox Code Playgroud)

我如何才能使i18n可以在函数内部工作而不必在要求内?

Server.js:

var    i18n = require('i18n-2');

global.i18n = i18n;
i18n.expressBind(app, {
    // setup some locales - other locales default to en silently
    locales: ['en', 'no'],
    // change the cookie name from 'lang' to 'locale'
    cookieName: 'locale'
});

app.use(function(req, res, next) {
    req.i18n.setLocaleFromCookie();
    next();
});

//CALL another file with some something here.
Run Code Online (Sandbox Code Playgroud)

otherfile.js:

somefunction() {
               message = i18n.__("no_user_to_select") + "???";

}
Run Code Online (Sandbox Code Playgroud)

我该如何解决?

javascript global internationalization node.js

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

redis randomKey 来自特定值?

我有很多不同的钥匙。有些以“用户 USERNAME”开头,还有许多其他。

有没有办法从“user *”获取随机密钥?

如果是这样,我怎样才能获得 5 个不同的用户?

redis

4
推荐指数
1
解决办法
2509
查看次数

使函数四舍五入到最接近的一刻钟

如何使函数四舍五入到最接近的 15 分钟,而不是四舍五入?如果是 10:46 则四舍五入到 11:00

jsfiddle:https ://jsfiddle.net/31L9d08s/

代码:

 var now = new Date();
    var Minutes = now.getMinutes();
    var quarterHours = Math.round(Minutes/15);
    if (quarterHours == 4)
    {
        now.setHours(now.getHours()+1);
    }
    var rounded = (quarterHours*15)%60;
    now.setMinutes(rounded);
    now.setSeconds(0);
    now.setMilliseconds(0);


    console.log(now.getTime());


    var currentdate = new Date(now.getTime()); 
    var datetime = "Last Sync: " + currentdate.getDate() + "/"
                    + (currentdate.getMonth()+1)  + "/" 
                    + currentdate.getFullYear() + " @ "  
                    + currentdate.getHours() + ":"  
                    + currentdate.getMinutes() + ":" 
                    + currentdate.getSeconds();


    console.log(datetime);
Run Code Online (Sandbox Code Playgroud)

javascript jquery

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

$ rootscope.$ emit/$ broadcast不会传递参数

我有以下内容:

   $rootScope.$on('setmoney',function (thisdata) {

            console.log("setting money");
            console.log("money is : " + thisdata);
});
Run Code Online (Sandbox Code Playgroud)

而在其他地方:

 $rootScope.$broadcast('setmoney', {cash: 100000});
Run Code Online (Sandbox Code Playgroud)

我怎样才能确保我能在setmoney rootscope.on函数中检索参数现金?

此数据后的输出:

Object {name: "setmoney", targetScope: Scope, defaultPrevented: false, currentScope: Scope}
currentScope
:
null
defaultPrevented
:
false
name
:
"setmoney"
preventDefault
:
()
targetScope
:
Scope
__proto__
:
Object
Run Code Online (Sandbox Code Playgroud)

broadcast angularjs rootscope

2
推荐指数
1
解决办法
1525
查看次数

编译时角度类型的防护错误

即时通讯开始一个新angular项目,但出现以下错误:

src / app / app-routing.module.ts(11,5)中的错误:错误TS2740:类型'typeof LoginGuard'缺少类型'any []'中的以下属性:pop,push,concat,join和25更多。

是什么typeofloginGuard acually究竟为何物,我需要做的,使因为它是用来工作的呢?

这是我的路线文件:

import { NgModule } from '@angular/core';
import { Routes, RouterModule } from '@angular/router';
import { LoginComponent } from "./login/login.component";
import { LoginlandingComponent} from "./loginlanding/loginlanding.component";
import { LoginGuard} from "./login.guard";

const routes: Routes = [
  { path: 'login', component: LoginComponent },
  {
    path: '',
    canActivateChild: LoginGuard,
    children: [
      {
        path: 'home',
        component: LoginlandingComponent
      }
    ]
  }

];

@NgModule({
  imports: [RouterModule.forRoot(routes)],
  exports: [RouterModule]
})
export class AppRoutingModule …
Run Code Online (Sandbox Code Playgroud)

javascript angular

2
推荐指数
1
解决办法
182
查看次数