2016-10-15 4 views
0

私はコードを実行すると、どこが間違っているのかわかりませんが、エラーres.json(res)は関数ではありません......あなたはresよりtopics.save()を行う際res.json()は関数ではありません... mongoose

私のNode.js、

exports.inserttopic = function (req, res) { 
    var topics = new Topics(req.body);console.log(topics) 
    topics.status = '1'; 
    topics.type = 'topics'; 
    var data = {}; 
    topics.save(function (err, res) { 
    if (err) { 
     console.log(err); 
     return err; 
    } 
    data = { status: false, error_code: 1, message: 'Unable to insert' }; 
    if (res) { 
     data = { status: true, error_code: 0, result: res, message: 'Inserted successfully' }; 
    } 
    res.json(data); 
    }); 
}; 

答えて

1

は別の変数名を使用してください。あなたのコードでは、元のresを上書きしているので、応答オブジェクトにアクセスすることはできません。

だから、これはあなたのコードは

exports.inserttopic = function (req, res) { 
    var topics = new Topics(req.body);console.log(topics) 
    topics.status = '1'; 
    topics.type = 'topics'; 
    var data = {}; 
    topics.save(function (err, mongooseResponse) { \\ Do not use res here 
    if (err) { 
     console.log(err); 
     return err; 
    } 
    data = { status: false, error_code: 1, message: 'Unable to insert' }; 
    if (mongooseResponse) { 
     data = { status: true, error_code: 0, result: mongooseResponse, message: 'Inserted successfully' }; 
    } 
    res.json(data); 
    }); 
}; 
+0

Thnkのようになります。あなたは非常に多くの....... – MMR

関連する問題