小编akc*_*soy的帖子

在mongoose中填充嵌套数组 - Node.js

这些是我的模式(主题是父级,包含'思想'的列表):

var TopicSchema = new mongoose.Schema({
  title: { type: String, unique: true },
  category: String,
  thoughts: [ThoughtSchema]
}, {
  timestamps: true,
  toObject: {virtuals: true},
  toJSON: {virtuals: true}
});

var ThoughtSchema = new mongoose.Schema({
  text: String,
  author: {type: mongoose.Schema.Types.ObjectId, ref: 'User'},
  votes:[{
    _id:false,
    voter: {type: mongoose.Schema.Types.ObjectId, ref: 'User'},
    up: Boolean,
    date: {type: Date, default: Date.now}
  }]
}, {
  timestamps: true,
  toObject: {virtuals: true},
  toJSON: {virtuals: true}
});

....
Run Code Online (Sandbox Code Playgroud)

我试图阅读思想的作者并改变我的获取主题api,如下所示:

...
  var cursor = Topic.find(query).populate({
    path: 'thoughts',
    populate: {
      path: 'author',
      model: …
Run Code Online (Sandbox Code Playgroud)

mongoose mongodb node.js

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

Spring MVC - HttpSession.setAttribute和model.addObject之间的区别

我最近正在努力学习Spring MVC.似乎我不太了解@ModelAttribute注释和HttpSession的功能.

@SessionAttributes({"shoppingCart", "count"})
public class ItemController {

@ModelAttribute("shoppingCart")
public List<Item> createShoppingCart() {
    return new ArrayList<Item>();
}

@ModelAttribute("count")
public Integer createCount() {
    return 0;
}

@RequestMapping(value="/addToCart/{itemId}", method=RequestMethod.GET)
public ModelAndView addToCart(@PathVariable("itemId") Item item, 
        @ModelAttribute("shoppingCart") List<Item> shoppingCart, @ModelAttribute("count") Integer count) {

    if(item != null) {
        shoppingCart.add(item);
        count = count + 1;
    }

    return new ModelAndView(new RedirectView("showAllItems")).addObject("count", count);
}

@RequestMapping(value="/deleteFromCart/{itemId}", method=RequestMethod.GET)
public ModelAndView deleteFromCart(@PathVariable("itemId") Item item, 
        HttpSession session) {

    List<Item> list = (List<Item>) session.getAttribute("shoppingCart");
    list.remove(item);
    //session.setAttribute("shoppingCart", list);

    Integer count = (Integer) session.getAttribute("count");
    count …
Run Code Online (Sandbox Code Playgroud)

spring session-variables spring-mvc httpsession modelattribute

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

如何使用Sum SQL和Spring Data JPA组?

我想按数量加载畅销产品.这些是我的表:

Product
id  name
1    AA   
2    BB

Productorder
order_id  product_id  quantity
1          1          10      
2          1          100    
3          2          15     
4          1          15       
Run Code Online (Sandbox Code Playgroud)

这是我的Spring Data Repository:

@Repository
public interface ProductRepository extends JpaRepository<Product, Long> {

    @Query(value = "select top 5 p.name, sum(po.quantity) as total_quantity from product p " +
            "inner join productorder po " +
                "on p.id = po.product_id " +
            "group by p.id, p.name " +
            "order by total_quantity desc", nativeQuery = true)
    List<Product> findTopFiveBestSeller();
}
Run Code Online (Sandbox Code Playgroud)

我收到HsqlException:找不到列:id

我认为这个错误与id列没有任何关系,因为它存在于两个表中."按分组查询分组"是否适用于Spring数据?因为对我来说这似乎有点奇怪,因为Spring …

sql spring jpql spring-data

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

如何在 Angular 中有条件地动画 :enter &amp; :leave 过渡?

我有一个列表,其中的项目有这样的动画:

<li @animation>
Run Code Online (Sandbox Code Playgroud)

这是我的动画触发器:

trigger('animation', [
  transition(':enter', [
    style({ height: '0px', 'padding-top': '0', 'padding-bottom': '0'}),  // initial
    animate('0.5s',
      style({ height: '*', 'padding-top': '*', 'padding-bottom': '*'}))  // final
  ]),
  transition(':leave', [
    style({ height: '*', 'padding-top': '*', 'padding-bottom': '*', opacity: 1}),  // initial
    animate('0.5s',
      style({ height: '0px', 'padding-top': '0', 'padding-bottom': '0', opacity: 0}))  // final
  ])
])
Run Code Online (Sandbox Code Playgroud)

如何有条件地为特定项目打开/关闭此动画?其实我在找…… 像这样:

<li [@animation]=item.isAnimated>
Run Code Online (Sandbox Code Playgroud)

这根本不起作用。

不幸的是,Angular 文档对此只有一句话:

对于进入或离开页面(从 DOM 中插入或删除)的元素,您可以使动画有条件。例如,将 *ngIf 与 HTML 模板中的动画触发器一起使用。

但是当我将动画注释与 *ngIf 结合起来时,非动画项目显然根本不会显示:

<li *ngIf="item.isAnimated" @animation>
Run Code Online (Sandbox Code Playgroud)

无论 isAnimated 标志如何,我都想进一步显示所有项目。我只想打开/关闭特定项目的动画。

angular angular-animations

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

注释@EnableSpringDataWebSupport不适用于WebMvcConfigurationSupport吗?

我已经使用WebMvcConfigurerAdapter已有一段时间了。由于无法使用getInterceptors()方法获取所有已注册的拦截器,因此我切换到了WebMvcConfigurationSupport,它具有许多默认的已注册Spring Bean,例如ContentNegotiationManager,ExceptionHandlerExceptionResolverusw。

现在,我已经意识到,尽管我在WebConfig类上使用了@EnableSpringDataWebSupport批注,但非常方便的DomainClassConverter(使用CrudRepository将域类ID转换为域类对象)并未默认注册。

当我像这样显式定义此bean时,它就会起作用。

@EnableSpringDataWebSupport
@Configuration
public class WebConfig extends WebMvcConfigurationSupport {
    @Bean
    public DomainClassConverter<?> domainClassConverter() {
        return new DomainClassConverter<FormattingConversionService>(mvcConversionService());
    }
}
Run Code Online (Sandbox Code Playgroud)

但是,为什么EnableSpringDataWebSupport无法与WebMvcConfigurationSupport一起使用?

spring spring-mvc spring-java-config spring-data-commons

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

没有找到处理yeoman-maven-plugin的市场条目

我最近安装并使用JHipster创建了一个应用程序.当我使用"mvn spring-boot:run"在终端中运行应用程序时,应用程序运行没有问题.

但是当我将项目(作为maven项目)导入Eclipse时,我的pom中出现了这个错误:

没有找到处理yeoman-maven-plugin的市场条目:0.4:在Eclipse中构建.有关更多信息,请参阅帮助.

这是错误的屏幕截图.

maven错误

这是默认情况下在生成的pom.xml中定义此插件的方式:

    <build>
        <plugins>
            <plugin>
                <groupId>com.github.trecloux</groupId>
                <artifactId>yeoman-maven-plugin</artifactId>
                <version>0.4</version>
                <executions>
                    <execution>
                        <id>run-grunt</id>
                        <phase>generate-resources</phase>
                        <goals>
                            <goal>build</goal>
                        </goals>
                        <configuration>
                            <skipTests>true</skipTests>
                            <buildTool>grunt</buildTool>
                            <buildArgs>compass:server --force</buildArgs>
                        </configuration>
                    </execution>
                </executions>
                <configuration>
                    <yeomanProjectDirectory>${project.basedir}</yeomanProjectDirectory>
                </configuration>
            </plugin>
        </plugins>
    </build>
Run Code Online (Sandbox Code Playgroud)

如何在Eclipse中继续操作,编辑生成的项目文件?

eclipse spring maven yeoman jhipster

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

Angular 5 动画:如何在组件从 DOM 中移除时使用 :leave 过渡

我们一直在我们的项目中使用@ng-bootstrap/ng-bootstrap 库来处理一些行为/组件,比如 Modal。最近我想消除这种依赖性并使用 Angular 实现引导模式行为。这实际上很容易。让我简单地告诉你它是如何工作的:

我有一个模态服务和一个模态组件。服务通过 ComponentFactoryResolver 动态创建模态组件(详细信息可以在这篇 SO post 中看到)并添加到 DOM 中。通过关闭模态,模态只是调用从服务中定义的回调函数,这只会破坏组件,从 DOM 中删除。

所以:我有这个模态组件的 2 个动画状态,进入和离开。输入效果很好。一旦组件出现在 dom 中,预定义的 :enter 状态就会被触发,我的动画就可以工作了。但是 :leave 没有。

这正是关闭模态的工作原理:模态是打开的,您单击关闭按钮或模态背景上的任何其他位置。这只是调用 close 函数,它被定义为一个输入,并在创建过程中从服务中给出。

@Input() closeCallback: Function;
Run Code Online (Sandbox Code Playgroud)

服务只是从 DOM 中删除组件。

由于单击关闭按钮后组件就会被删除,因此我认为动画没有它需要的时间。所以 :leave 不起作用。

我想关闭超时(延迟),并手动触发动画,但由于我想使用预定义的行为:输入和:离开,我无法弄清楚这是怎么可能的。那么我怎样才能让我的离开动画工作呢?(有或没有:离开)

服务代码:

@Injectable()
export class ModalService implements OnDestroy {

  private renderer: Renderer2;
  private componentRef: ComponentRef<ModalComponent>;

  constructor(private rendererFactory: RendererFactory2,
              private componentFactoryResolver: ComponentFactoryResolver,
              private appRef: ApplicationRef,
              private injector: Injector) {
    this.renderer = rendererFactory.createRenderer(null, null);
  }

  ngOnDestroy() {
    this.componentRef.destroy();
  }

  open(content: string, titel: string, primaryButtonLabel: …
Run Code Online (Sandbox Code Playgroud)

angular angular-animations

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

Nosql数据库设计-MongoDB

我正在尝试构建一个只有以下3种模型的应用程序:

  • topic (has just a title (max 100 chars.))
  • comment (has text (may be very long), author_id, topic_id, createdDate)
  • author (has just a username)

Actually a very simple db structure. A Topic may have many comments, which are created by authors. And an author may have many comments.

I am still trying to figure out the best way of designing the database structure (documents). First I though to put everything to its own schema like above. …

database-design mongodb nosql

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

如何在 TypeScript 中使用动态键定义对象类型?

我有一个 const 对象,如下所示:

export const Language: Values = {
  es: {urlValue: 'es', label: 'Spain'},
  en: {urlValue: 'en', label: 'Anything'},
  eb: {urlValue: 'eb', label: 'Other thing'},
};

export interface Values {
  [name: string]: Value;
}

export interface Value {
  urlValue;
  label;
  selected?;
}
Run Code Online (Sandbox Code Playgroud)

现在在应用程序的某个地方,我们读取了一些具有这种格式的数据,这些数据恰好具有完全相同的键:

{
  es: 32,
  en: 11,
  eb: 56
}
Run Code Online (Sandbox Code Playgroud)

所以实际上我需要另一种对象类型,如下所示:

export class AnotherObject {

  constructor(public es: number,
              public en: number,
              public eb: number) {
  }
}
Run Code Online (Sandbox Code Playgroud)

但是我可以动态创建这种类型,以便它自动使用 Language 对象的键,或“Value”类型的 urlValue 吗?

PS:问题是我们确切用例的简化形式,其中我们有多个常量,例如语言。所以自动化会非常有帮助。

编辑:对于@basarat的建议 - 由于该对象是父对象的一部分,我实际上需要在这部分中定义类型:

export class …
Run Code Online (Sandbox Code Playgroud)

typescript

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

CSS flex-wrap:除非包裹,否则在项目之间放置间隙?

我经常遇到这种情况,即我有 2 个 flex-children,具有属性 flex-direction row。因此它们首先并排显示,之间有间隙(右边距)。

通过调整大小,一旦没有足够的空间容纳两个子元素,flex-wrap 就会将第二个子元素移动到第一个子元素下方,所以我不再需要 1. item 的 margin-right 了。

我可以根据“换行”状态动态设置边距吗?

小提琴: https: //jsfiddle.net/ejmhxztd/

小提琴注意:您应该调整窗口大小(减少宽度)并查看包装好的情况。如果继续减小宽度,您将看到第一个子元素的文本也将分成两行,因为右边距在那里并占用空间。

.parent {
  display:flex;
  flex-direction:row;
  flex-wrap:wrap
}

.child1 {
  margin-right:300px
}
Run Code Online (Sandbox Code Playgroud)
<div class="parent">
   <span class="child1">Child1 Text</span>
   <span class="child2">Child2 Text</span>
</div>
Run Code Online (Sandbox Code Playgroud)

html css flexbox

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