小编cha*_*nie的帖子

如果一个promise从一个.fail()里面返回一个错误,那就传播一个拒绝

我有以下代码:

var myFunc = function(id, obj, backupObj) {
  return update(obj) // this returns a promise
  .fail(function(err){
    // if update is rejected
    restore(id, backupObj); // i wish to run this
    // and return the error that 'update(obj)' threw and propagate it
    return Q.reject(err); // should it be thrown like this???
  });
}
Run Code Online (Sandbox Code Playgroud)

这是处理update()错误并返回错误的正确方法,所以当myFunc()从任何地方调用它时,它可以传播并最终处理?例如,在链中执行:

var foo = function(id, obj) {
  var backupObj = {};

  return checkIfValidObj(obj)
  .then(function(_backupObj) {
    backupObj = _backupObj;
    return doSomethingElse(id, obj);
  })
  .then(function() {
    // …
Run Code Online (Sandbox Code Playgroud)

javascript node.js promise q

5
推荐指数
0
解决办法
39
查看次数

信息隐藏和封装之间有什么区别?

我知道由于研究存在差异,但我只能发现它们之间的相似之处...我希望有人能澄清这些差异,如果你能为每个人做一个例子真的有帮助.Java程序也请将此程序视为封装或信息隐藏,甚至两者兼而有之

 class DogsinHouse {
   private int dogs;
   public int getdog() {
     return dogs;
   }

   public void setdog(int amountOfDogsNow) {
     dogs = amountOfDogsNow;
   }
 }
Run Code Online (Sandbox Code Playgroud)

java oop encapsulation

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

Angular 库中提供的 HttpInterceptor 无法在 Angular 应用程序中运行

我正在开发一个 Angular 库,其中有一个提供HttpInterceptor. 主要思想是让这个拦截器在导入此身份验证模块的任何应用程序中自动工作,而无需对其进行任何额外的设置。

到目前为止我所拥有的是以下内容:

认证模块

@NgModule({
  imports: [ConcreteAuthModule],
  providers: [
    {
      provide: HTTP_INTERCEPTORS,
      useClass: BearerInterceptor,
      multi: true
    }
  ]
})
export class AuthenticationModule {
  static forRoot(config: AuthConfig): ModuleWithProviders {
    return {
      ngModule: AuthenticationModule,
      providers: [
        {
          provide: AUTH_CONFIG,
          useValue: config
        }
      ]
    };
  }
}
Run Code Online (Sandbox Code Playgroud)

具体验证模块

@NgModule({
  imports: [ThirdPartyLibModule],
  providers: [
    {
      provide: AuthenticationService,
      useClass: ConcreteAuthService
    }
  ]
})
export class ConcreteAuthModule { }
Run Code Online (Sandbox Code Playgroud)

承载拦截器

@Injectable()
export class BearerInterceptor implements HttpInterceptor {
  constructor(private authService: AuthenticationService) { } …
Run Code Online (Sandbox Code Playgroud)

javascript typescript angular-http-interceptors angular

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

HOW-TO:Angular 库分为独立的特征子库

我目前正在开发一个 Angular 库(在Angular v 8.1.0 中)并想知道如何将它“拆分到不同的子库中”

例如,Angular ( @angular) 以某种方式拆分为不同的“部分”,例如:

  • @angular/common
  • @angular/core
  • @angular/forms
  • 等等。

例如,@angular/common有不同的子目录可供导入:

  • @angular/common/http
  • @angular/common/locale
  • @angular/common/testing

我想知道如何在我自己的 Angular 库中进行这种结构化,以保持不同的上下文元素彼此独立。假设我的库被调用,cool-lib并且有不同的功能模块包含它们自己的服务/组件/等:common, featureA, featureB.

我想实现这样的目标:

import { CommonService } from 'cool-lib/common';
import { FeatAComponent, FeatAService } from 'cool-lib/feature-a';
import { FeatBModule } from 'cool-lib/feature-b';
Run Code Online (Sandbox Code Playgroud)

而不是:

import { 
  CommonService, 
  FeatAComponent, 
  FeatAService,
  FeatBModule
} from 'cool-lib';
Run Code Online (Sandbox Code Playgroud)

甚至能够独立地改变不同功能模块的版本号,或者只安装那些将需要的模块而不必安装整个库(假设我只需要featureA)也很好。

例如,在我的package.json 上

{
  "dependencies": {
    "cool-lib/common": "1.0.0",
    "cool-lib/feature-a": "1.0.0", …
Run Code Online (Sandbox Code Playgroud)

javascript typescript angular angular-library ng-packagr

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

从Spring Service使用AJAX GET下载文件

我正在尝试实现一个自动开始下载所请求文件的服务.

这是我的AJAX电话:

function downloadFile(fileName) {
  $.ajax({
    url : SERVICE_URI + "files/" + fileName,
    contentType : 'application/json',
    type : 'GET',
    success : function (data)
    {
      alert("done!");
    },
    error: function (error) {
      console.log(error);
    }
  });
}
Run Code Online (Sandbox Code Playgroud)

这是我的Spring Service方法GET:

@RequestMapping(value = "/files/{file_name}", method = RequestMethod.GET)
public void getFile(@PathVariable("file_name") String fileName,
                    HttpServletResponse response) {
    try {
        // get your file as InputStream
        FileInputStream fis = new FileInputStream( fileName + ".csv" );
        InputStream is = fis;
        // copy it to response's OutputStream
        ByteStreams.copy(is, response.getOutputStream());
        response.setContentType("text/csv"); …
Run Code Online (Sandbox Code Playgroud)

java ajax jquery spring file-upload

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

TypeScript:接口多态问题

我有一个基本帐户界面:

interface Account {
  id: number;
  email: string;
  password: string;
  type: AccountType;
}
Run Code Online (Sandbox Code Playgroud)

其中帐户类型

enum AccountType {
  Foo = 'foo',
  Bar = 'bar'
}
Run Code Online (Sandbox Code Playgroud)

以及扩展Account接口的两个帐户子类型(FooAccountBarAccount ) :

interface FooAccount extends Account {
  foo: Foo;
}
Run Code Online (Sandbox Code Playgroud)
interface BarAccount extends Account {
  bar: Bar;
}
Run Code Online (Sandbox Code Playgroud)

Account是保存基本帐户信息的聚合,并且根据类型拥有FooBar对象。

对这些对象的操作只能由其所有者(帐户)执行。

我定义了一个AccountRepository

export interface AccountRepository {
  findById(accountId: number): Account;
}
Run Code Online (Sandbox Code Playgroud)

其中findById(accountId: number)返回一个Account,但此帐户可以是任何FooAccountBarAccount。 …

javascript oop typescript

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

使用Spring Framework和jquery-upload-file插件问题上传文件

我无法通过AJAX从我的网络客户端上传文件到我的服务器.我在客户端使用以下jQuery库来进行文件上传:https://github.com/hayageek/jquery-upload-file

In the server-side, I'm using Spring Framework and I have followed the following Spring Tutorial to build my Service: https://spring.io/guides/gs/uploading-files/

At first, my server method looked like this (file was defined as @RequestParam):

@RequestMapping(value="/upload", method=RequestMethod.POST)
public @ResponseBody String handleFileUpload(@RequestParam("file") MultipartFile file){
    //functionality here
}
Run Code Online (Sandbox Code Playgroud)

but every time I submitted the Upload form I got a Bad Request message from the Server, and my handleFileUpload() method was never called.

After that, …

ajax spring file-upload jquery-file-upload

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

Mongoose - find(): 搜索选项中的对象不起作用

我有一个看起来像这样的猫鼬模式:

var mySchema = new mongoose.Schema({
  ...
  metadata: {
    isDeleted: {
      type: Boolean,
      default: false
    },
    ...
  }
});
Run Code Online (Sandbox Code Playgroud)

我想获取应用过滤器的mongodb数据库中的元素列表,所以我有以下对象:

var searchOptions = { metadata: { isDeleted: false } };
Run Code Online (Sandbox Code Playgroud)

在执行查询之前,始终需要将该metadata.isDeleted值设置为false,与稍后将添加的其他参数分开:

var objQuery = myModel.find(searchOptions, '-metadata');
Run Code Online (Sandbox Code Playgroud)

起初,我在架构中的对象isDeleted之外有属性metadata,并且

var searchOptions = { isDeleted: false };
Run Code Online (Sandbox Code Playgroud)

used to work perfectly. But it is since I decided to have isDeleted inside my metadata object that is not working and can't figure out why...

javascript mongoose mongodb node.js mongodb-query

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