简体   繁体   English

req.session.cart [object object] [object object] 如何在我的 sapper/svelte 应用程序中迭代和查看它

[英]req.session.cart [object object] [object object] how to iterate and view it in my sapper/svelte application

night owls: So I'm practicing my sapper/svelte application.夜猫子:所以我正在练习我的工兵/苗条应用程序。 Created a shopping page with few items where I can order those items and add them to my shopping cart.创建了一个包含少量商品的购物页面,我可以在其中订购这些商品并将它们添加到我的购物车中。

Everything is working (I add items to the cart in the session) but the items I add are stored in the session (db session) as [object object] [object object] and I don't know how to view that despite the fact I know how the object is designed.一切正常(我在会话中将项目添加到购物车)但我添加的项目作为 [object object] [object object] 存储在会话(db session)中,尽管事实上我不知道如何查看我知道对象是如何设计的。 I read all the tuts out there on how to iterate over an object and all that, yet, I'm unable to display the req.session.cart objects in my application.我阅读了所有关于如何迭代对象以及所有这些的 tuts,但是,我无法在我的应用程序中显示 req.session.cart 对象。 I don't know if I'm doing it right or wrong.我不知道我这样做是对还是错。 Please advise.请指教。

my svelte page with the shopping items are basic and no need to add it here.我的带有购物项目的细长页面是基本的,无需在此处添加。 Here is my server route where I capture the ordered item and process those entries.这是我的服务器路由,我在其中捕获订购的项目并处理这些条目。

SC3.JS file where I fetch the data:我在其中获取数据的 SC3.JS 文件:

    var fetch = require('node-fetch');

export async function post(req, res, next) {

    res.setHeader('Content-Type', 'application/json');
    /* Retrieve the data */
    var data = req.body;

    //cart constructor
    function Cart() {        

        this.add = function (item) {            
            this.item = item;
            let title = this.item.title;
            let price = this.item.price;


            let storeditem = { title: title, price: price };
            req.session.cart += { item: storeditem };
            console.log("session log: ", req.session.cart);     

        }; 
    }

    var cart = new Cart();
    cart.add(data);


    /* Returns the result */
    return res.end(JSON.stringify({ info: data }));

}

my mongodb is showing the following我的 mongodb 显示以下内容在此处输入图片说明

Am I adding the items wrong by doing req.session.cart += storeditem ???我是否通过 req.session.cart += storeditem 添加了错误的项目??? If this is the right way to store the items, how do I retrieve them with [object object] nested inside the session cart object?如果这是存储项目的正确方法,我如何使用嵌套在会话购物车对象中的 [object object] 检索它们?

I tried obj.keys, obj.values etc from the es6 new object methods but it all it showed was some keys but not the actual items (title and price) that I added to the cart.我尝试了 es6 新对象方法中的 obj.keys、obj.values 等,但它显示的只是一些键,而不是我添加到购物车的实际项目(标题和价格)。

This is a sapper/svelte app which is pure html, css and js Any help would be appreciated.这是一个工兵/苗条的应用程序,它是纯 html、css 和 js 任何帮助将不胜感激。

You are adding the items wrong, you cannot += an object.您添加的项目错误,您不能 += 一个对象。 (this will cast it to string) (这会将它转换为字符串)

Here we can reproduce your bug:我们可以在这里重现您的错误:

 let cart = {}; let storeditem = { title: 'my title', price: 1.23 }; cart += { item: storeditem }; console.log("session log: ", cart);

And here is how we can fix that:这是我们如何解决这个问题:

 let cart = {}; let storeditem = { title: 'my title', price: 1.23 }; let id = 'you need an id'; // Maybe you can use item.id ??? cart[id] = { item: storeditem }; console.log("session log: ", cart);

the problem is this line here: req.session.cart += { item: storeditem };问题是这里的这一行: req.session.cart += { item: storeditem };

you are appending { item: storeditem } to the cart property with += which is converting it into a string, and since it's an object it's getting turned into [object object]您正在使用 += 将 { item: storeditem } 附加到购物车属性,这会将其转换为字符串,并且由于它是一个对象,因此它正在变成 [object object]

I'm not sure on the structure of your "cart" property, but if it's an array, just change that line to我不确定“cart”属性的结构,但如果它是一个数组,只需将该行更改为

req.session.cart.push({ item: storeditem })

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM