我正在尝试将商品添加到我的购物车中,该商品使用改造从android应用程序存储在express-session中。但是购物车数量保持不变。如何设置标题以访问快速会话?
我正在使用Nodejs开发后端的电子商务应用程序,并将购物车存储在快速会话中。至于客户端(Android应用程序),我正在使用改型来获取API数据。使用邮递员测试路线时,项目已成功添加。但是当我尝试使用改造并通过它进行设置时,数量始终为1。我相信原因是未设置会话。
我认为没有必要在客户端上创建新的数据存储:因为会话数据存储在服务器端。但是express-session id存储在cookie中,如果我正确的话,cookies存储在客户端。如果这是错误的方法,您会提出什么建议?如果不是,我的错误是什么?
购物车模型-cart.js
int strncasecmp(const char *s1, const char *s2, int n)
{
if (n && s1 != s2)
{
do {
int d = tolower(*s1) - tolower(*s2);
if (d || *s1 == '\0' || *s2 == '\0') return d;
s1++;
s2++;
} while (--n);
}
return 0;
}
ProductDetails.java
module.exports = function Cart(oldCrart) {
this.items = oldCrart.items || {}; //if oldcart is not diggined use empity as default
this.totalQty = oldCrart.totalQty || 0;
this.totalPrice = oldCrart.totalPrice || 0;
this.add = function(item, id) {
var storedItem = this.items[id];
if (!storedItem) {
storedItem = this.items[id] = { item: item, qty: 0, price: 0 };
}
storedItem.qty++;
storedItem.price = storedItem.item.price * storedItem.qty;
this.totalQty++;
this.totalPrice += storedItem.item.price * 1; //*1 is there to treat the variable as intiger
};
this.generateArray = function() {
var arr = [];
for (var id in this.items) {
arr.push(this.items[id]);
}
return arr;
};
};
客户端api
compositeDisposable.add(ClientApi.addToCart(Productid, "session Id")
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribe(
new Consumer<Cart>() {
@Override
public void accept(Cart cart) throws Exception {
Toast.makeText(SingleProduct.this, "Product added to cart"), Toast.LENGTH_SHORT).show();
}
}, new Consumer<Throwable>() {
@Override
public void accept(Throwable throwable) throws Exception {
Toast.makeText(SingleProduct.this,throwable.getMessage() , Toast.LENGTH_SHORT).show();
}
}));
}
addToCart路线
@GET("/mobile-api/add-to-cart/{id}")
Observable<Cart> addToCart(@Path("id")String pid, @Header("cookie.connect.sid") String sid);
我使用邮差和android应用程序添加了四次相同的产品 邮递员回来了
//serverside
router.get("/mobile-api/add-to-cart/:id", function(req, res) {
var productId = req.params.id;
var cart = new Cart(req.session.cart ? req.session.cart : {}); //pass the cart if it exist or empity if it doesnt
console.log("cart", cart);
Product.findById(productId, function(err, product) {
if (err) {
console.log(err);
}
cart.add(product, product.id);
const totalQty = (req.session.cart = cart);
var Mycart = new Cart(req.session.cart);
const cartData = {
products: Mycart.generateArray(),
totalPrice: Mycart.totalPrice
};
console.log(cart);//LOG THE CART
res.json(cartData);
});
});
返回控制台,但android应用返回
cart Cart {
items:
{ '5c871fa5a9f2e1fc0ec63eaa': { item: [Object], qty: 4, price: 18220 } },
totalQty: 4,
totalPrice: 18220
totalQty和qty始终为1
答案 0 :(得分:0)
我启用了RetrofitClient类以接受cookie,它的工作原理就像一个魅力
public class RetrofitClient {
private static Retrofit instance;
public static Retrofit getInstance(){
//create OKHTTPCLIENT
HttpLoggingInterceptor interceptor = new HttpLoggingInterceptor();
interceptor.setLevel(HttpLoggingInterceptor.Level.BODY);
CookieHandler cookieHandler = new CookieManager();
OkHttpClient client = new OkHttpClient.Builder().addNetworkInterceptor(interceptor)
.cookieJar(new JavaNetCookieJar(cookieHandler))
.connectTimeout(10, TimeUnit.SECONDS)
.writeTimeout(10, TimeUnit.SECONDS)
.readTimeout(30, TimeUnit.SECONDS)
.build();
if(instance == null)
instance= new Retrofit.Builder()
.baseUrl("http://192.168.44.125:5000")
.addConverterFactory(GsonConverterFactory.create())
.addCallAdapterFactory(RxJava2CallAdapterFactory.create())
.client(client)
.build();
return instance;
}
}