API通过2个字段获取查询,使用MongoDB将空对象发送到React App中的控制器

时间:2018-07-19 05:32:39

标签: mongodb reactjs mern

我有一个带有MongoDB Notes集合的React应用。我希望查询返回2个字段(member_id:XXXXXXX和product_id:YYYYYY)的数据,我正在将请求从Notes.js文件发送到API.js(数据通过)到服务器端的notesController.js。我所有其他请求均正常工作,因此文件和连接均正常工作,但在此请求下,我无法将数据从API.js获取到notesController.js(notes控制器中的console.log显示未定义),因此mongoDB会将其读取为findAll 这是我的代码现在的样子:

Notes.js - I also tried this as (adding a second param to the API.js to 
   take in the member data)
  API.getNote(this.props.match.params.id, {
     member:this.state.member._id})  
 but member was undefined in the API.js console.log

Code as it is now
var myMember = {
   product: this.props.match.params.id,
   member:this.state.member._id}
var myJSON = JSON.stringify(myMember)
console.log(myJSON)
API.getNote(myJSON)
   .then(res => {
      this.setState({
         note: res.data
      })
   })
 .catch(err => console.log(err))
 }

API.js file (console.log shows the JSON object)


getNote: function (ids) {
   console.log("API.js" + ids)
   return axios.get("/api/notes/" + ids);
}

notesController.js file  (console.log is undefined)

find: function (req, res) {
   console.log("body " + req.params.ids);
   db.Note
     .find (req.params.ids)
        // what I want the query to look like 
        // .find({ product_id: YYYYY, member_id: XXXX })
     .sort({ date: -1 })
     .then(dbModel => res.json(dbModel))
     .catch(err => res.status(422).json(err));
}

0 个答案:

没有答案