Problem to create items with shopping cart
I solved some issues here but I got this message :
Reference.update failed: First argument contains undefined in property 'shopping-carts.-LRvqLfzwv9q9OMfW23K.items.-LPa0C_lNj2VAe1xLF5S.product.title'
The root not created.
export class ShoppingCartService {
constructor(private db: AngularFireDatabase) { }
private create() {
return this.db.list('/shopping-carts').push({
dateCreated: new Date().getTime()
});
}
private getCart(cartId: string) {
return this.db.object('/shopping-carts/' + cartId);
}
getItem(cartId: string, productId: string) {
return this.db.object('/shopping-carts/' + cartId + '/items/' + productId);
}
private async getOrCreateCartId() {
const cartId = localStorage.getItem('cartId');
if (cartId) { return cartId; }
const result = await this.create();
localStorage.setItem('cartId', result.key);
return result.key;
}
async addToCart(product) {
const cartId = await this.getOrCreateCartId();
const item$ = this.getItem(cartId, product.key);
item$.snapshotChanges().take(1).subscribe((item: any) => {
if (item.payload.exists()) {
item$.update({ product: product, quantity: item.payload.val().quantity + 1});
} else {
item$.update({ product: {
title: product.title,
price: product.price,
category: product.category,
imageUrl: product.imageUrl,
}, quantity: 1} );
}
});
}
angular
add a comment |
I solved some issues here but I got this message :
Reference.update failed: First argument contains undefined in property 'shopping-carts.-LRvqLfzwv9q9OMfW23K.items.-LPa0C_lNj2VAe1xLF5S.product.title'
The root not created.
export class ShoppingCartService {
constructor(private db: AngularFireDatabase) { }
private create() {
return this.db.list('/shopping-carts').push({
dateCreated: new Date().getTime()
});
}
private getCart(cartId: string) {
return this.db.object('/shopping-carts/' + cartId);
}
getItem(cartId: string, productId: string) {
return this.db.object('/shopping-carts/' + cartId + '/items/' + productId);
}
private async getOrCreateCartId() {
const cartId = localStorage.getItem('cartId');
if (cartId) { return cartId; }
const result = await this.create();
localStorage.setItem('cartId', result.key);
return result.key;
}
async addToCart(product) {
const cartId = await this.getOrCreateCartId();
const item$ = this.getItem(cartId, product.key);
item$.snapshotChanges().take(1).subscribe((item: any) => {
if (item.payload.exists()) {
item$.update({ product: product, quantity: item.payload.val().quantity + 1});
} else {
item$.update({ product: {
title: product.title,
price: product.price,
category: product.category,
imageUrl: product.imageUrl,
}, quantity: 1} );
}
});
}
angular
add a comment |
I solved some issues here but I got this message :
Reference.update failed: First argument contains undefined in property 'shopping-carts.-LRvqLfzwv9q9OMfW23K.items.-LPa0C_lNj2VAe1xLF5S.product.title'
The root not created.
export class ShoppingCartService {
constructor(private db: AngularFireDatabase) { }
private create() {
return this.db.list('/shopping-carts').push({
dateCreated: new Date().getTime()
});
}
private getCart(cartId: string) {
return this.db.object('/shopping-carts/' + cartId);
}
getItem(cartId: string, productId: string) {
return this.db.object('/shopping-carts/' + cartId + '/items/' + productId);
}
private async getOrCreateCartId() {
const cartId = localStorage.getItem('cartId');
if (cartId) { return cartId; }
const result = await this.create();
localStorage.setItem('cartId', result.key);
return result.key;
}
async addToCart(product) {
const cartId = await this.getOrCreateCartId();
const item$ = this.getItem(cartId, product.key);
item$.snapshotChanges().take(1).subscribe((item: any) => {
if (item.payload.exists()) {
item$.update({ product: product, quantity: item.payload.val().quantity + 1});
} else {
item$.update({ product: {
title: product.title,
price: product.price,
category: product.category,
imageUrl: product.imageUrl,
}, quantity: 1} );
}
});
}
angular
I solved some issues here but I got this message :
Reference.update failed: First argument contains undefined in property 'shopping-carts.-LRvqLfzwv9q9OMfW23K.items.-LPa0C_lNj2VAe1xLF5S.product.title'
The root not created.
export class ShoppingCartService {
constructor(private db: AngularFireDatabase) { }
private create() {
return this.db.list('/shopping-carts').push({
dateCreated: new Date().getTime()
});
}
private getCart(cartId: string) {
return this.db.object('/shopping-carts/' + cartId);
}
getItem(cartId: string, productId: string) {
return this.db.object('/shopping-carts/' + cartId + '/items/' + productId);
}
private async getOrCreateCartId() {
const cartId = localStorage.getItem('cartId');
if (cartId) { return cartId; }
const result = await this.create();
localStorage.setItem('cartId', result.key);
return result.key;
}
async addToCart(product) {
const cartId = await this.getOrCreateCartId();
const item$ = this.getItem(cartId, product.key);
item$.snapshotChanges().take(1).subscribe((item: any) => {
if (item.payload.exists()) {
item$.update({ product: product, quantity: item.payload.val().quantity + 1});
} else {
item$.update({ product: {
title: product.title,
price: product.price,
category: product.category,
imageUrl: product.imageUrl,
}, quantity: 1} );
}
});
}
export class ShoppingCartService {
constructor(private db: AngularFireDatabase) { }
private create() {
return this.db.list('/shopping-carts').push({
dateCreated: new Date().getTime()
});
}
private getCart(cartId: string) {
return this.db.object('/shopping-carts/' + cartId);
}
getItem(cartId: string, productId: string) {
return this.db.object('/shopping-carts/' + cartId + '/items/' + productId);
}
private async getOrCreateCartId() {
const cartId = localStorage.getItem('cartId');
if (cartId) { return cartId; }
const result = await this.create();
localStorage.setItem('cartId', result.key);
return result.key;
}
async addToCart(product) {
const cartId = await this.getOrCreateCartId();
const item$ = this.getItem(cartId, product.key);
item$.snapshotChanges().take(1).subscribe((item: any) => {
if (item.payload.exists()) {
item$.update({ product: product, quantity: item.payload.val().quantity + 1});
} else {
item$.update({ product: {
title: product.title,
price: product.price,
category: product.category,
imageUrl: product.imageUrl,
}, quantity: 1} );
}
});
}
export class ShoppingCartService {
constructor(private db: AngularFireDatabase) { }
private create() {
return this.db.list('/shopping-carts').push({
dateCreated: new Date().getTime()
});
}
private getCart(cartId: string) {
return this.db.object('/shopping-carts/' + cartId);
}
getItem(cartId: string, productId: string) {
return this.db.object('/shopping-carts/' + cartId + '/items/' + productId);
}
private async getOrCreateCartId() {
const cartId = localStorage.getItem('cartId');
if (cartId) { return cartId; }
const result = await this.create();
localStorage.setItem('cartId', result.key);
return result.key;
}
async addToCart(product) {
const cartId = await this.getOrCreateCartId();
const item$ = this.getItem(cartId, product.key);
item$.snapshotChanges().take(1).subscribe((item: any) => {
if (item.payload.exists()) {
item$.update({ product: product, quantity: item.payload.val().quantity + 1});
} else {
item$.update({ product: {
title: product.title,
price: product.price,
category: product.category,
imageUrl: product.imageUrl,
}, quantity: 1} );
}
});
}
angular
angular
edited Nov 22 '18 at 15:07
user1618811
asked Nov 20 '18 at 15:37
user1618811user1618811
65
65
add a comment |
add a comment |
2 Answers
2
active
oldest
votes
try change this line :
item$.update({ product: product, quanity: (item.payload.val.quanity +
1 })
with
item$.update({ product: product, quanity: (item.payload.val.quanity + 1) })
you missing the ')' when your update your object.
can not read quanity ?
– user1618811
Nov 20 '18 at 15:56
what you mean you can't read quanity ?
– Muhammad Al Faris
Nov 21 '18 at 2:09
I need to update the object with new property "quanity" but it gave me errors.
– user1618811
Nov 22 '18 at 13:23
[enter image description here][1] [1]: i.stack.imgur.com/pGvWR.png
– user1618811
Nov 22 '18 at 13:33
could you give me the value of item.payload.val() ?
– Muhammad Al Faris
Nov 22 '18 at 22:23
|
show 1 more comment
Finally , I solve it and I like to share with all
export class ShoppingCartService {
constructor(private db: AngularFireDatabase) { }
private create() {
return this.db.list('/shopping-carts').push({
dateCreated: new Date().getTime()
});
}
private getCart(cartId: string) {
return this.db.object('/shopping-carts/' + cartId);
}
getItem(cartId: string, productId: string) {
return this.db.object('/shopping-carts/' + cartId + '/items/' + productId);
}
private async getOrCreateCartId() {
const cartId = localStorage.getItem('cartId');
if (cartId) { return cartId; }
const result = await this.create();
localStorage.setItem('cartId', result.key);
return result.key;
}
async addToCart(product) {
const cartId = await this.getOrCreateCartId();
const item$ = this.getItem(cartId, product.key);
console.log(item$);
item$.snapshotChanges().take(1).subscribe((item: any) => {
if (item.payload.exists()) {
item$.update({quantity: item.payload.val().quantity + 1 });
} else {
item$.update({
product: {
title: product.payload.val().title,
price: product.payload.val().price,
category: product.payload.val().category,
imageUrl: product.payload.val().imageUrl,
}, quantity: 1
});
}
});
}
}
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53396469%2fproblem-to-create-items-with-shopping-cart%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
try change this line :
item$.update({ product: product, quanity: (item.payload.val.quanity +
1 })
with
item$.update({ product: product, quanity: (item.payload.val.quanity + 1) })
you missing the ')' when your update your object.
can not read quanity ?
– user1618811
Nov 20 '18 at 15:56
what you mean you can't read quanity ?
– Muhammad Al Faris
Nov 21 '18 at 2:09
I need to update the object with new property "quanity" but it gave me errors.
– user1618811
Nov 22 '18 at 13:23
[enter image description here][1] [1]: i.stack.imgur.com/pGvWR.png
– user1618811
Nov 22 '18 at 13:33
could you give me the value of item.payload.val() ?
– Muhammad Al Faris
Nov 22 '18 at 22:23
|
show 1 more comment
try change this line :
item$.update({ product: product, quanity: (item.payload.val.quanity +
1 })
with
item$.update({ product: product, quanity: (item.payload.val.quanity + 1) })
you missing the ')' when your update your object.
can not read quanity ?
– user1618811
Nov 20 '18 at 15:56
what you mean you can't read quanity ?
– Muhammad Al Faris
Nov 21 '18 at 2:09
I need to update the object with new property "quanity" but it gave me errors.
– user1618811
Nov 22 '18 at 13:23
[enter image description here][1] [1]: i.stack.imgur.com/pGvWR.png
– user1618811
Nov 22 '18 at 13:33
could you give me the value of item.payload.val() ?
– Muhammad Al Faris
Nov 22 '18 at 22:23
|
show 1 more comment
try change this line :
item$.update({ product: product, quanity: (item.payload.val.quanity +
1 })
with
item$.update({ product: product, quanity: (item.payload.val.quanity + 1) })
you missing the ')' when your update your object.
try change this line :
item$.update({ product: product, quanity: (item.payload.val.quanity +
1 })
with
item$.update({ product: product, quanity: (item.payload.val.quanity + 1) })
you missing the ')' when your update your object.
answered Nov 20 '18 at 15:45
Muhammad Al FarisMuhammad Al Faris
363
363
can not read quanity ?
– user1618811
Nov 20 '18 at 15:56
what you mean you can't read quanity ?
– Muhammad Al Faris
Nov 21 '18 at 2:09
I need to update the object with new property "quanity" but it gave me errors.
– user1618811
Nov 22 '18 at 13:23
[enter image description here][1] [1]: i.stack.imgur.com/pGvWR.png
– user1618811
Nov 22 '18 at 13:33
could you give me the value of item.payload.val() ?
– Muhammad Al Faris
Nov 22 '18 at 22:23
|
show 1 more comment
can not read quanity ?
– user1618811
Nov 20 '18 at 15:56
what you mean you can't read quanity ?
– Muhammad Al Faris
Nov 21 '18 at 2:09
I need to update the object with new property "quanity" but it gave me errors.
– user1618811
Nov 22 '18 at 13:23
[enter image description here][1] [1]: i.stack.imgur.com/pGvWR.png
– user1618811
Nov 22 '18 at 13:33
could you give me the value of item.payload.val() ?
– Muhammad Al Faris
Nov 22 '18 at 22:23
can not read quanity ?
– user1618811
Nov 20 '18 at 15:56
can not read quanity ?
– user1618811
Nov 20 '18 at 15:56
what you mean you can't read quanity ?
– Muhammad Al Faris
Nov 21 '18 at 2:09
what you mean you can't read quanity ?
– Muhammad Al Faris
Nov 21 '18 at 2:09
I need to update the object with new property "quanity" but it gave me errors.
– user1618811
Nov 22 '18 at 13:23
I need to update the object with new property "quanity" but it gave me errors.
– user1618811
Nov 22 '18 at 13:23
[enter image description here][1] [1]: i.stack.imgur.com/pGvWR.png
– user1618811
Nov 22 '18 at 13:33
[enter image description here][1] [1]: i.stack.imgur.com/pGvWR.png
– user1618811
Nov 22 '18 at 13:33
could you give me the value of item.payload.val() ?
– Muhammad Al Faris
Nov 22 '18 at 22:23
could you give me the value of item.payload.val() ?
– Muhammad Al Faris
Nov 22 '18 at 22:23
|
show 1 more comment
Finally , I solve it and I like to share with all
export class ShoppingCartService {
constructor(private db: AngularFireDatabase) { }
private create() {
return this.db.list('/shopping-carts').push({
dateCreated: new Date().getTime()
});
}
private getCart(cartId: string) {
return this.db.object('/shopping-carts/' + cartId);
}
getItem(cartId: string, productId: string) {
return this.db.object('/shopping-carts/' + cartId + '/items/' + productId);
}
private async getOrCreateCartId() {
const cartId = localStorage.getItem('cartId');
if (cartId) { return cartId; }
const result = await this.create();
localStorage.setItem('cartId', result.key);
return result.key;
}
async addToCart(product) {
const cartId = await this.getOrCreateCartId();
const item$ = this.getItem(cartId, product.key);
console.log(item$);
item$.snapshotChanges().take(1).subscribe((item: any) => {
if (item.payload.exists()) {
item$.update({quantity: item.payload.val().quantity + 1 });
} else {
item$.update({
product: {
title: product.payload.val().title,
price: product.payload.val().price,
category: product.payload.val().category,
imageUrl: product.payload.val().imageUrl,
}, quantity: 1
});
}
});
}
}
add a comment |
Finally , I solve it and I like to share with all
export class ShoppingCartService {
constructor(private db: AngularFireDatabase) { }
private create() {
return this.db.list('/shopping-carts').push({
dateCreated: new Date().getTime()
});
}
private getCart(cartId: string) {
return this.db.object('/shopping-carts/' + cartId);
}
getItem(cartId: string, productId: string) {
return this.db.object('/shopping-carts/' + cartId + '/items/' + productId);
}
private async getOrCreateCartId() {
const cartId = localStorage.getItem('cartId');
if (cartId) { return cartId; }
const result = await this.create();
localStorage.setItem('cartId', result.key);
return result.key;
}
async addToCart(product) {
const cartId = await this.getOrCreateCartId();
const item$ = this.getItem(cartId, product.key);
console.log(item$);
item$.snapshotChanges().take(1).subscribe((item: any) => {
if (item.payload.exists()) {
item$.update({quantity: item.payload.val().quantity + 1 });
} else {
item$.update({
product: {
title: product.payload.val().title,
price: product.payload.val().price,
category: product.payload.val().category,
imageUrl: product.payload.val().imageUrl,
}, quantity: 1
});
}
});
}
}
add a comment |
Finally , I solve it and I like to share with all
export class ShoppingCartService {
constructor(private db: AngularFireDatabase) { }
private create() {
return this.db.list('/shopping-carts').push({
dateCreated: new Date().getTime()
});
}
private getCart(cartId: string) {
return this.db.object('/shopping-carts/' + cartId);
}
getItem(cartId: string, productId: string) {
return this.db.object('/shopping-carts/' + cartId + '/items/' + productId);
}
private async getOrCreateCartId() {
const cartId = localStorage.getItem('cartId');
if (cartId) { return cartId; }
const result = await this.create();
localStorage.setItem('cartId', result.key);
return result.key;
}
async addToCart(product) {
const cartId = await this.getOrCreateCartId();
const item$ = this.getItem(cartId, product.key);
console.log(item$);
item$.snapshotChanges().take(1).subscribe((item: any) => {
if (item.payload.exists()) {
item$.update({quantity: item.payload.val().quantity + 1 });
} else {
item$.update({
product: {
title: product.payload.val().title,
price: product.payload.val().price,
category: product.payload.val().category,
imageUrl: product.payload.val().imageUrl,
}, quantity: 1
});
}
});
}
}
Finally , I solve it and I like to share with all
export class ShoppingCartService {
constructor(private db: AngularFireDatabase) { }
private create() {
return this.db.list('/shopping-carts').push({
dateCreated: new Date().getTime()
});
}
private getCart(cartId: string) {
return this.db.object('/shopping-carts/' + cartId);
}
getItem(cartId: string, productId: string) {
return this.db.object('/shopping-carts/' + cartId + '/items/' + productId);
}
private async getOrCreateCartId() {
const cartId = localStorage.getItem('cartId');
if (cartId) { return cartId; }
const result = await this.create();
localStorage.setItem('cartId', result.key);
return result.key;
}
async addToCart(product) {
const cartId = await this.getOrCreateCartId();
const item$ = this.getItem(cartId, product.key);
console.log(item$);
item$.snapshotChanges().take(1).subscribe((item: any) => {
if (item.payload.exists()) {
item$.update({quantity: item.payload.val().quantity + 1 });
} else {
item$.update({
product: {
title: product.payload.val().title,
price: product.payload.val().price,
category: product.payload.val().category,
imageUrl: product.payload.val().imageUrl,
}, quantity: 1
});
}
});
}
}
export class ShoppingCartService {
constructor(private db: AngularFireDatabase) { }
private create() {
return this.db.list('/shopping-carts').push({
dateCreated: new Date().getTime()
});
}
private getCart(cartId: string) {
return this.db.object('/shopping-carts/' + cartId);
}
getItem(cartId: string, productId: string) {
return this.db.object('/shopping-carts/' + cartId + '/items/' + productId);
}
private async getOrCreateCartId() {
const cartId = localStorage.getItem('cartId');
if (cartId) { return cartId; }
const result = await this.create();
localStorage.setItem('cartId', result.key);
return result.key;
}
async addToCart(product) {
const cartId = await this.getOrCreateCartId();
const item$ = this.getItem(cartId, product.key);
console.log(item$);
item$.snapshotChanges().take(1).subscribe((item: any) => {
if (item.payload.exists()) {
item$.update({quantity: item.payload.val().quantity + 1 });
} else {
item$.update({
product: {
title: product.payload.val().title,
price: product.payload.val().price,
category: product.payload.val().category,
imageUrl: product.payload.val().imageUrl,
}, quantity: 1
});
}
});
}
}
export class ShoppingCartService {
constructor(private db: AngularFireDatabase) { }
private create() {
return this.db.list('/shopping-carts').push({
dateCreated: new Date().getTime()
});
}
private getCart(cartId: string) {
return this.db.object('/shopping-carts/' + cartId);
}
getItem(cartId: string, productId: string) {
return this.db.object('/shopping-carts/' + cartId + '/items/' + productId);
}
private async getOrCreateCartId() {
const cartId = localStorage.getItem('cartId');
if (cartId) { return cartId; }
const result = await this.create();
localStorage.setItem('cartId', result.key);
return result.key;
}
async addToCart(product) {
const cartId = await this.getOrCreateCartId();
const item$ = this.getItem(cartId, product.key);
console.log(item$);
item$.snapshotChanges().take(1).subscribe((item: any) => {
if (item.payload.exists()) {
item$.update({quantity: item.payload.val().quantity + 1 });
} else {
item$.update({
product: {
title: product.payload.val().title,
price: product.payload.val().price,
category: product.payload.val().category,
imageUrl: product.payload.val().imageUrl,
}, quantity: 1
});
}
});
}
}
answered Nov 24 '18 at 19:40
user1618811user1618811
65
65
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53396469%2fproblem-to-create-items-with-shopping-cart%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown