我正在尝试将对象数组的第一个位置内的属性传递给另一个模块,以便稍后可以使用该值。我尝试将它作为 module(args) 传递,但它一直读取默认值 0。有没有办法做到这一点?
我尝试实现一些 React.context
但 Bot 框架模拟器拒绝它。
/////////////////Module that ll acquire the value/////////////////////////////
getCard(bot, builder, params) {
let configValues = { ...params[0] }
bot.dialog(`${configValues.path}`, function (session) {
var msg = new builder.Message(session);
const cardItem = (obj) => {
return (new builder.HeroCard(session)
.title(`${obj.title}`)
.text(`R$ ${obj.price}`)
.images([builder.CardImage.create(session, `${obj.img}`)])
.buttons([
builder.CardAction.imBack(session, `${obj.price} Item adicionado!`, 'add to cart')
// !onClick event must add the current obj.price to
// the configValues.total(Ex: configValues.total += obj.price)!
])
)
}
msg.attachmentLayout(builder.AttachmentLayout.carousel)
msg.attachments(
eval(params.map(obj => cardItem(obj)))
);
//!in here before end the dialog is where i want to update
// the configValues.total so i can show it in the -> Checkout module
session.send(msg).endDialog()
}).triggerAction({ matches: configValues.regex });
}
}
//////////////CheckOut.Module///////////////////////////////
{...}
let configValues = { ...params[0] }
let state = {
nome: "",
endereco: "",
pagamento: "",
total: configValues.total // this is the value to be read
}
bot.dialog('/intent', [
{...},
(session, results) => {
state.pagamento = results.response
session.send(
JSON.stringify(state) // here is the place to be printed
)
{...}
]
).triggerAction({ matches: /^(finalizar|checar|encerrar|confirmar pedido|terminar)/i })
最佳答案
既然您解决了最初的问题,我将回答您评论中的问题。
您的问题在这里:
cartId.map((obj, i , arr) => {
// if (!obj.total) {
// obj.total.reduce(i => i += i)
// }
const newtotal = new total
newtotal.getTotals(bot, builder, obj, arr)
})
cartId
包含每件商品的总计。当您对其调用 map
时,您会将每个项目单独传递给 getTotals
,后者将每个项目传递给 checkout()
您无法对所有总计进行求和且只能对一项的总计进行求和的原因是您将 cartId
传递给 checkout
和 cartId
已更改为仅单个项目。相反,您可以执行以下几项不同的操作:
从
cartItems
传递整个cartId
并在totalConstructor( )
和checkoutConstructor()
。这可能是最简单的,但内存效率不高。使用BotBuilder's State Storage将
totals
数组存储在userData
中,然后在最后求和。这可能更难实现,但会是一条更好的路线。 Here's a sample这可以帮助您入门。
关于javascript - 如何创建类似 `context.Provider`/`context.Consumer` 的结构来在机器人应用程序中传递值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55541982/