2017-08-21 7 views
1

私は私のAPIファイルに投稿機能を持っています。私はこのポストの要求を使用してのMongoDBデータベースに電子メールを保存したい:ポストapiでmongodbにデータを保存

rest-api/emails?from="[email protected]"&"to"="[email protected]"&subject="some subject"&content="some more content"&provider=sendgrid&success=false 

私は方法は、以下のようなものです投稿:

.post((req, res)=>{ 

    db.models.Email.create({from:req.body.from , 
          to: req.body.to, 
          subject: req.body.subject, 
          content: req.body.content, 
          success: req.body.success, 
          provider: req.body.provider}, (err, result)=>{ 
    if(err){ 
     handleErrors(res, err); 
     console.log(`error in creating Email :${err}`) 
    }else { 
     console.log() 
     res.json(result) 
    } 
    }); 
}); 

だけ_idsuccessproviderフィールドはMongoDBの中に保存し、このPOSTリクエストの後に。 どうすればこの問題を解決できますか?

EDIT: 私はポスト要求から「すべてを削除するが、まだ正しく保存されない:

localhost:3000/rest-api/[email protected]&[email protected]&subject=somesubject&content=somemorecontent&provider=sendgrid&success=false 

RES:

{ 
    "__v": 0, 
    "_id": "599add615fcb202b34c6a13e", 
    "success": false, 
    "provider": "sendgrid" 
} 

EDIT: モデル:

var ValidationError = mongoose.Error.ValidationError; 
var Schema = mongoose.Schema; 

mongoose.Promise = global.Promise ; 

var validateEmail = function(email) { 
    if (email) return validator.isEmail(email); 
    return true; 
} 

var EmailSchema = new Schema({ 
     from: {type: String, maxlength:100}, 
     to: {type: String, maxlength:100}, 
     subject: {type: String}, 
     content: {type: String}, 
     provider: {type: String, maxlength: 30, required: false, default: "sendgrid"}, 
     sentdate: { type: Date }, 
     success: {type: Boolean, default: false} 
}); 
EmailSchema.pre('save',(next)=>{ 
    // if(!this.sentdate){ 
    // this.sentdate = Date.now(); 
    // } 
    next(); 
}); 

EmailSchema.plugin(mongoosePaginate); 
var Email = mongoose.model('Email',EmailSchema); 




module.exports ={ 
    models:{ 
    Email 
    }, 
    url: 'mongodb://localhost/emilapp', 
} 

req.bodyコンソール: {} req.bodyが空です!

+0

あなたが持つことができないので、 '' "あなたのURLで あなたは純粋なテキスト値を送信する必要がありますあなたのURLはすべての ''を削除します'それが動作するはずです – sheplu

+0

req.bodyのメールモデルとconsole.logを追加して質問を編集できますか? –

+0

@ArpitSolanki編集 – amir

答えて

1

req.bodyはPOSTリクエストデータのために使用されることを意味しています。サーバーを表現するために送られたクエリ文字列はreq.queryオブジェクトに格納されている。あなたは、以下のようなものに我々のコードを変更する必要があります。

.post((req, res)=>{ 

    db.models.Email.create({from:req.query.from , 
          to: req.query.to, 
          subject: req.query.subject, 
          content: req.query.content, 
          success: req.query.success, 
          provider: req.query.provider}, (err, result)=>{ 
    if(err){ 
     handleErrors(res, err); 
     console.log(`error in creating Email :${err}`) 
    }else { 
     console.log() 
     res.json(result) 
    } 
    }); 
}); 
0

あなたのあなたは文字列の間で「入れない要求は以下のようにする必要がありますGETしてください:あなたはGETリクエストとしてデータを送信しているのに対し、

rest-api/[email protected]&[email protected]&subject=some 
subject&content=some more content&provider=sendgrid&success=false 
関連する問題