Javascript 如何将模型(类)方法添加到水线中的直通(关联)模型?
给定一个带有字段的直通(关联)模型,我需要在其中添加一些模型方法 关联模型: 当我尝试访问这个方法时,我遇到了一个错误:c.incrementQ不是一个函数 : 为什么没有模型方法 如何扩展关联模型Javascript 如何将模型(类)方法添加到水线中的直通(关联)模型?,javascript,node.js,sails.js,waterline,Javascript,Node.js,Sails.js,Waterline,给定一个带有字段的直通(关联)模型,我需要在其中添加一些模型方法 关联模型: 当我尝试访问这个方法时,我遇到了一个错误:c.incrementQ不是一个函数 : 为什么没有模型方法 如何扩展关联模型 p.S.这是github。在当前模型配置中incrementQ是模型方法,而不是实例。实例方法在属性中声明。哦,是的!非常感谢。让我纠正一下。我的意思是模型方法——总共接下来的问题:)1。您确定测试用例中的c是模型而不是模型的实例吗?2.如果c是模型,您是否确定已正确创建该模型并添加了方法incre
p.S.这是github。在当前模型配置中
incrementQ
是模型方法,而不是实例。实例方法在属性中声明。哦,是的!非常感谢。让我纠正一下。我的意思是模型方法——总共接下来的问题:)1。您确定测试用例中的c
是模型而不是模型的实例吗?2.如果c
是模型,您是否确定已正确创建该模型并添加了方法incrementQ
?若你们用测试的设置代码来更新这个问题,它会更清晰。好的,我看到它是一个模型,而不是模型上的实例。我会很快更新它。泰,让我们来。
var BBPromise = require('bluebird');
module.exports = {
attributes: {
quantity: { type: 'integer' },
//Relations
a: { model: 'a', foreignKey: true, columnName: 'a_id' },
workday: { model: 'b', type: 'date', foreignKey: true, columnName: 'b_id' }
},
//Instance method
incrementQ: BBPromise.promisify(function (id, inc, cb) {
C.findOne(id).then(function (found) {
if (!found) {
return cb({ status: 404, message: "didn't found the id" });
}
found.quantity += inc;
console.log('AAAAAAAAAAAAA', found);
return found.save(cb);
}).catch(cb);
})
};
var assert = require('chai').assert,
destroyCollections = require('../../../helper').destroyCollections;
describe('C', function () {
var idOfC;
before(function (done) {
B.create({ id: DateTimeService.generateNowDate() })
.then(function (newB) {
A.create({ name: "my new a" }).then(function (newA) {
c.create({ a_id: newA.id, b_id: newB.id, quantity: 12 }).then(function (newC) {
idOfC = newC.id;
done();
}).catch(done);
}).catch(done);
}).catch(done);
});
after(function (done) {
destroyCollections(A, B, C, done);
});
it('incrementQ()', function (done) {
console.log('THE MODEL', c);
console.log('THE MODEL method', c.incrementQ);
c.incrementQ(idOfC, -2).then(function (result) {
assert.equal(result.quantity, 10, "quantity decremented");
done();
}).catch(done);
});
});