我需要从承诺中获取变量的价值,但我不能因为其范围。在我的情况下,我使用Sequelize将数据保存到SQL数据库,但是我想要使用类和dataToReturn
我收到的save
在第二个.post('/', (req, res) => {
const event = new Event(Events, Data, req.body)
event.save()
res.status(201).json("HERE I NEED THE 'dataToReturn' from the promise")
})
之后收到<{1}}
class Event {
constructor(Events, Data, Event) {
this._Data = Data,
this._Events = Events
this._Event = Event
}
save() {
this._Data.build(this._Event.Data).save().then((res) =>
this._Events.build({
name: this._Event.name,
date: this._Evento.fecha,
photo1: this._Event.photo1,
photo2: this._Event.photo2,
idData: res.dataValues.id
}).save().then((dataToReturn) => "???")) <-- dont know what to do here
return dataToReturn <-- I want to return that variable but I cant because scopes
}
这是我的课堂活动
delimiter #
create trigger TABLE3_INSERT_TRIGGER after insert on table2
for each row
begin
insert into table3 (tableZ_ID, table2_ID) values (new.tableZ_ID, new.table2_ID);
end#
delimiter ;
你推荐什么? 我迷失了
答案 0 :(得分:1)
在手机上写这个,所以它不会看起来最好......
在您的活动中删除then。删除return语句。在您的路线中:
return event.save().then(results => res.status(200).json(results));
答案 1 :(得分:1)
$x
然后在您的路线中使用 class Event {
/**
* Same code
*/
save() {
return this._Data.build(this._Event.Data).save().then((res) =>
this._Events.build({
name: this._Event.name,
date: this._Evento.fecha,
photo1: this._Event.photo1,
photo2: this._Event.photo2,
idData: res.dataValues.id
})).save();
}
:
then