在我的个人资料模型中,我有这个方法:
// Check login and return object
checkLogin: function(username, password) {
ProfileObj = Profile.find({username: username, password: password}).one(function(err, profile){
return profile;
});
console.log(ProfileObj);
return ProfileObj;
}
要调用此方法,我使用:
profile = req.models.profile();
ProfileObj = profile.checkLogin('test', 'test');
不幸的是,我没有得到我想要的对象,我得到的是模型,而不是实例。 find()回调中的“profile”var是我想要的,但不会返回。相反,ProfileObj是整个模型。
{ find: [Function],
only: [Function],
limit: [Function],
skip: [Function],
offset: [Function],
order: [Function],
orderRaw: [Function],
count: [Function],
remove: [Function],
first: [Function],
last: [Function],
each: [Function],
run: [Function],
success: [Function],
fail: [Function],
all: [Function],
where: [Function],
one: [Function],
beforeCreate: [Function],
afterCreate: [Function],
beforeSave: [Function],
afterSave: [Function],
beforeValidation: [Function],
beforeRemove: [Function],
afterRemove: [Function],
afterLoad: [Function],
afterAutoFetch: [Function],
extendsTo: [Function],
model:
{ [Function]
allProperties:
{ username: [Object],
name: [Object],
email: [Object],
password: [Object],
id: [Object] },
settings: { set: [Function], get: [Function], unset: [Function] },
drop: [Function],
sync: [Function],
get: [Function],
find: [Function],
all: [Function],
one: [Function],
count: [Function],
aggregate: [Function],
exists: [Function],
create: [Function],
clear: [Function],
beforeCreate: [Function],
afterCreate: [Function],
beforeSave: [Function],
afterSave: [Function],
beforeValidation: [Function],
beforeRemove: [Function],
afterRemove: [Function],
afterLoad: [Function],
afterAutoFetch: [Function],
hasOne: [Function],
hasMany: [Function],
extendsTo: [Function] },
options:
{ only: [ 'id', 'username', 'name', 'email', 'password' ],
id: [ 'id' ],
table: 'profile',
driver:
{ config: [Object],
opts: [Object],
customTypes: {},
query: [Object],
db: [Object],
aggregate_functions: [Object],
uid: '2d7089091970609caded514e621b51a7' },
conditions: { username: 'test', password: 'test' },
associations: [],
limit: undefined,
order: null,
merge: null,
offset: undefined,
newInstance: [Function] } }
您能否告诉我如何正确返回find()回调中的“个人资料”。
由于
答案 0 :(得分:0)
由于node.js使用异步代码,因此无法实现。
答案 1 :(得分:0)
您可以在模型中创建一个方法,如下所示:
<form onSubmit={this.stopSubmit}>
<FormGroup
controlId="formBasicText"
validationState={getValidationState()}
>
<ControlLabel>Stuff</ControlLabel>
<FormControl
type="text"
value={this.state.value}
placeholder="Enter text"
onChange={handleChange}
onKeyUp={handleSubmit.bind(this, "test")}
/>
<FormControl.Feedback/>
<HelpBlock>Validation is based on string length.</HelpBlock>
</FormGroup>
</form>
由于理论上您提供了表单提交中的用户名和密码,因此您可以执行以下操作:
models.profile = db.define('profile', {
id: { type: 'serial', key: true },
another_column: { type: 'text' }
}, {
methods: {
checkLogin: function (username, password) {
models.profile.one({ username, password }, (err, user) => {
return user;
});
}
}
}
祝你好运!