简体   繁体   中英

mongoose syntax with user.id connecting to a form

Hi I am new to mongoose and with angular through ionic. I am trying to build an app and having problem with it. I have no Idea if this syntax mongoose is right because it still is not working in my front end where the user picks and answer and it saves it to the database

Heres my mongoose question.js

var express = require('express');
var router = express.Router();
var mongoose = require('mongoose');
var Question = require('../models/Question.js');
var app = express();




 // GET questions listing. questions/yourroute
 router.get('/', function(req, res, next) { 
 Question.find(function(err, questions){
 if(err) return next(err);

 res.json(questions);

 });
 });

 router.post('/saved-questions', function(req, res){

 var now = Date()
 User.findById(user.id, function(err, user){

var newanswer1 = {
    question: '58ccb991caf9756f8e1d8a6c',
    answer_text: req.body.Q1,
    postdate: now
 }



 });


 user.users_answers.push(newanswer1);

 //repeat for Q 2 and 3, push each time

 User.save()

  });
  module.exports = router;

And this is the form in ionic.. I don't know why its not working, maybe its how im putting it in the ionic form. There suppose to be slides of questions in ionic and each possible answer is in the ion check box

 <div id="questions" ng-controller="QCntrl">

 <ion-content>
 <ion-slide-box>
 <ion-slide>
 <div class="intro" ng-controller="PopupCtrl">
    <h1 class="hidden">Intro Question</h1>
    <h3>Just need to ask some questions to make this app personalized for you</h3>
    <button class="button" value="next" ng-click="nextSlide()">OKAY</button>
     <button class="button2 button-positive" ng-click="PopAlert()">
  Why?
     </button>

   </div>

  </ion-slide>




  <ion-slide>
  <div id="Q1box">
  <h2>{{questions[0].question_text}}</h2>
 <ul>
 <li data-ng-repeat="answer in answers1">
 <ion-checkbox id="Q1" ng-model="filter.users_answers">{{answer.label}}</ion-    checkbox>
</li>
</ul>
<a href="#" ng-click="next()">Next Question</a>
</div>
</ion-slide> 

Where you have User.save() , try changing it to user.save and move the }) that's before User.save and put it after where you change User.save to user.save

You are editing the user you just found with User.findById(user.id, function(err, user){ and it seems you then want to save the push you made to user.users_answers which user.save should do for you

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM