D
D
deadkEEper12015-11-23 01:02:32
Node.js
deadkEEper1, 2015-11-23 01:02:32

How to properly fetch a model?

And so, there is a backbone view for all users. Near each user there is a button linking to the user/_id backbone route

<a href="#user/<%=user._id%>"> 
<button> more </button>

Here is the BB router
define([
  'backbone',
  './views/homePageView',
  './views/logInView',
  './views/userView',
  './models/userModel'
    ],

  function(Backbone, homePageView, logInView, userView, User) {


    var Router = Backbone.Router.extend({

      routes: {
        ''     : 'homepage',
        'logIn': 'login',
        'user/:id': 'findUser'
      },

      initialize: function(){
        console.log('router inited')
        Backbone.history.start()
      },

      homepage : function(){
         new homePageView;
      },

      login: function(){
         new logInView;

      },

      findUser: function(id){
         		console.log(id)

            var user = new User()
            user.fetch({_id: id})
            console.log(user)
      }
    })
    return  Router

  })

On the user/:id route, I want to get a specific user. The findUser function takes an argument with a bang. But when I try to fetch the model. Browser complains
GET localhost:3030/user 404 (Not Found)
Here is the server router
var express = require('express');

var router = express.Router();



// router.use('/signIn',      require('./signInRouter'))
router.use('/login', 	   require('./logInRouter'))
router.use('/sessiontest', require('./sessionTestRouter'))
router.use('/logOut', 	   require('./logOutRouter'))
 router.use('/user',  	   require('./userRouter'))                                 ///ТУТ
router.use('/users',       require('./showAllUsers'))

module.exports = router

./userRouter.js
var express = require('express');
var UserMongo = require('../mongoModels/userMongo')

var userRouter = express.Router();





userRouter.route('/:id')
  .get(function(req, res){
    console.log(req.params)
    res.send()
  })


module.exports = userRouter

And in it, in theory, I should find a model with _id = req.params.id in the database, but since the browser gives an error, I can’t get req.params.id Tell me
what I did wrong. Thank you.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
E
Evgeny Petrov, 2015-11-23
@EvgenZZ

1.
test.user=Backbone.Model.extend({
initialize: function(options) {
this.options=options;
//this.fetch();
},
url: function() {
switch (this.options.mode) {
case 0:{return this.options.host+'user/id/'+this.options.selector;}
}
},
parse: function(response) {
switch (this.options.mode){
case 0:{
var entry =new Object();
entry=response[0];
return entry;
}
}
}
});
2. in the controller or function test.user.fetch().done(function(response){'here you look at the response from the server'})
3. Or
Admin.SessionModel=Backbone.Model.extend({
urlRoot:'check',
initialize: function () {
var that = this;
// Hook into jquery
// Use withCredentials to send the server cookies
// The server must allow this through response headers
$.ajaxPrefilter(function( options, originalOptions, jqXHR) {
options. xhrFields = {
withCredentials: true
};
});
},
login: function(creds) {
// Do a POST to /api/session and send the serialized form creds
var that = this;
this.save(creds, {
success: function (model, resp) {
if (resp.success == false) {
alert(resp.message);
}
that.unset('password');
that.set(resp.data );
iApp.trigger("login:action:success");
iApp.radio.trigger('login_form:close');
},
error:function(model,resp){
iApp.trigger("login:action:error");
console.log('error')
}
});
},
logout: function() {
// Do a DELETE to /api/session and clear the client side data
var that = this;
this.destroy({
success: function (model, resp) {
model.clear({silent:true});
$.ajax({url:'logout'}).done(function(){
iApp.trigger("logout:action");
console.log('App log out')
})
// Set auth to false to trigger a change:logged_in event
// The server also returns a new csrf token so that
// the user can relogin without refreshing the page
that.set ({logged_in: false});
}
});
},
getAuth: function(callback) {
// getAuth is wrapped around our router
// before we start any routers let us see if the user is valid
var that=this;
this.fetch().done(function (data) {
that.set('status', data.status);
//console.log(data)
return data;
});
}
})
4. or if you need explanations, write in a personal

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question