【问题标题】:How to pass input for a post to mongo using clicked and expressjs如何使用clicked and express js将帖子的输入传递给mongodb
【发布时间】:2017-03-19 07:44:01
【问题描述】:

我想在我的数据库中使用以下猫鼬模型,我想知道如何从引导页面获取输入并将其发布到 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)
            );
    }
}

【问题讨论】:

    标签: mongodb angular express post click


    【解决方案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=''
    

    【讨论】:

    • 但是我如何做到这一点有多个输入框以获取详细信息,然后有一个提交按钮来发布输入框的内容?我要使用的模型中大约有 5 个字符串,是否必须为每个字符串提供服务?
    • 这是您的电话。如果你觉得 5 个单独的跟注会很快,否则等待并结合所有 5 个跟注。
    • 好的,谢谢,最后一个问题,如果我通过一次调用添加员工并且模型的构造函数获取了用户输入的上面列出的所有元素,我是否让他们进入商店并且它会自动在该商店中查找匹配的商店名称,如果有,它将引用它,如果没有,它会抛出错误或设置为 null?
    • 一旦订阅了store,对应store的变化会自动通知订阅的对象。因此,如果您正在创建一个新项目,请将其推送到服务并将数据发送到商店,商店会根据需要进行更新
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-01-17
    • 2014-07-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多