如何使用clicked和expressjs

时间:2017-03-18 23:35:57

标签: mongodb angular express post click

我想在我的数据库中使用以下mongoose模型,我想知道如何从bootstrap页面获取输入,将其发布到mongo。

var mongoose = require('mongoose');
var Schema = mongoose.Schema;
var muv = require('mongoose-unique-validator');
var schema = new Schema({
    firstName: {type: String, required: true},
    lastName: {type: String, required: true},
    empId: {type: String, required: true, unique: true},
    num: {type: String, required: true},
    job: {type: String, required: true},
    store: {type: Schema.ObjectId, ref: 'Stores'},
});

schema.plugin(muv);

module.exports = mongoose.model('Message', schema);

我尝试引用组件中使用的html模板中的元素,这些模板将使用服务与expressjs进行交互,但它不起作用。

import { Component, OnInit } from '@angular/core';
import { Message } from "./message.model";
import { MessageService } from "./message.service";

@Component({
  moduleId: module.id,
  selector: 'deta',
  templateUrl: 'details.component.html',
  providers: [MessageService]
})
export class DetailsComponent implements OnInit {
   messages: Message[] = [];

    constructor(private messageService: MessageService) {}

    ngOnInit() {
        this.messageService.getMessages()
            .subscribe(
                messages => this.messages = messages,
                error => console.error(error)
            );
    }

    onAddMessage() {
        const message = new Message(' It still worked!');
        this.messages.push(message);
        this.messageService.saveMessage(message)
            .subscribe(
                () => console.log('Success!'),
                error => console.error(error)
            );
    }
}

1 个答案:

答案 0 :(得分:1)

您应该将ngModel用作

<input type="text" [(ngModel)]="inputMessage" /> 

方法应该是

onAddMessage() {

        this.messages.push(this.inputMessage);
        this.messageService.saveMessage(this.messages)
            .subscribe(
                () => console.log('Success!'),
                error => console.error(error)
            );
    }

将变量声明为

inputMessage:string=''