Backbone.js 无法进行收集。请在主干网中提取+;打字稿
我正在使用主干网构建应用程序,但无法有效地执行collection.fetch。有两个具体问题:Backbone.js 无法进行收集。请在主干网中提取+;打字稿,backbone.js,typescript,Backbone.js,Typescript,我正在使用主干网构建应用程序,但无法有效地执行collection.fetch。有两个具体问题: 我需要使用fetch查询传递一些值,以便接收查询的控制器可以基于这些值构造SQL查询 我希望能够从持久性数据库中获取适当的值,并将它们返回到集合,以便使集合保持最新 这是我的密码: //模型 class QuestionModel extends Backbone.Model{ defaults(){ return{ type:true,
class QuestionModel extends Backbone.Model{
defaults(){
return{
type:true,
question:" "
};
}
idAttribute = "question";
url = "question";
}
//收藏
class SurveyCollection extends Backbone.Collection {
constructor(options) {
super(options);
var self = this;
this.on("add", function (model) {
var self = model;
model.save({}, {
success: function () {
Log.success("Saved " + self.get("question"));
app.views.indexView.render();
},
error: function () {
Log.failure("Saved " + self.get("question"));
}
});
}, this);
this.fetch();
}
model = QuestionModel;
url = "survey";
}
//在视图中提出获取请求
this.collection.fetch({
data:{
values: 100,
type: "normal"
},
success: function(collection, response) {
_.each(collection.models, function(model) {
Log.show(model.get("question"));
})
},
error: function(err){
Log.error("couldn't receive data");
}
});
控制器:
index(req, res) {
log.Log.show("Received index request : values = " + req.body.values + " type : " + req.body.type);
sqlConnector.query('select * from question', function (err, rows, fields) {
if (!err) {
_.each(rows, function (row){
log.Log.show("row : " + row);
})
res.send(rows);
} else {
log.Log.show("error : " + err);
res.status(500).send(err);
}
});
}
目前MySQL数据库包含4个问题值:
John
Jacob
加载页面时(即调用render()和fetch()时)
当我添加另一个值“Jasmine”并再次调用fetch时:
the browser console shows 2 values:
Jasmine
Jasmine
Jasmine
the server console shows
Received index request : values = undefined type : undefined
show rows; [object Object]
show rows; [object Object]
show rows; [object Object]
the browser console shows 2 values:
Jeremy
Jeremy
Jeremy
Jeremy
the server console shows
Received index request : values = undefined type : undefined
show rows; [object Object]
show rows; [object Object]
show rows; [object Object]
如果我添加另一个值“Jeremy”并再次调用提取:
the browser console shows 2 values:
Jasmine
Jasmine
Jasmine
the server console shows
Received index request : values = undefined type : undefined
show rows; [object Object]
show rows; [object Object]
show rows; [object Object]
the browser console shows 2 values:
Jeremy
Jeremy
Jeremy
Jeremy
the server console shows
Received index request : values = undefined type : undefined
show rows; [object Object]
show rows; [object Object]
show rows; [object Object]
我需要解决这个问题,并确保两端都提取了正确的值除了这是用TypeScript编写的以外,这主要是主干的问题?有一个建议是,您可以使用函数箭头语法(
=>
)进行回调,因为它使处理这个
变得简单得多。我通常建议您在代码中设置一个断点,看看哪里出了问题。您的意思是像success=>function()。这似乎不起作用是的,这是一个可能有帮助的例子。它使处理这个
更加可靠。您建议的语法不起作用:success:(collection,response)=>{}
和(model)=>{}
两者都应该可以正常工作。你试了什么/在哪里?