我无法将我的产品对象推入购物车阵列中,我不知道该怎么办。 有人知道如何解决吗? 我收到此错误:无法为不可为空的字段Mutation.addToShopping返回null。
用户模型:
def take_attendance(request, id):
schedule = get_object_or_404(ScheduleTable, pk=id)
userlist = User.objects.filter(college = schedule.college).filter(is_student=True).filter(section=schedule.section)
context = {
"userlist":userlist,
"schedule_name": schedule,
}
return render(request, "take.html", context )
def AttendanceStore(request, id):
sch = get_object_or_404(ScheduleTable, pk=id)
userlist = User.objects.all().filter(college=sch.college).filter(section=sch.section)
attendance = OnlineAttendanceTable()
insert_list = []
for user in userlist:
userstatus = request.POST.get(str(user.username))
attendance.schedule = sch.name
attendance.usn = user.username
attendance.status = userstatus
insert_list.append(attendance)
print(insert_list)
OnlineAttendanceTable.objects.bulk_create()
return render(request, "submitattendance.html" )
解析器:
import mongoose from 'mongoose';
const userSchema = new mongoose.Schema({
name: {
type: String,
required: true,
unique: true
},
email: {
type: String,
required: true,
unique: true
},
password: {
type: String,
required: true
},
cart: {
type: Array,
}
});
userSchema.set('toObjenct', { viruals: true });
var Users = mongoose.model('User', userSchema);
module.exports = Users;
模式:
addToShopping: async (parent, args, context, info) => {
const userId = getUserId(context);
const product = await Products.findById(args.id);
console.log(product)
const user = await Users.findByIdAndUpdate(userId, { $push: { cart: product } }, { new: true }).exec()
.catch((err) => {
console.log(err)
});
return user;
},
答案 0 :(得分:0)
Tnx我找到了答案:)
addToShopping: async (parent, args, context, info) => {
const userId = await getUserId(context);
const product = await Products.findById(args.id);
const values = {};
Object.entries(args).forEach(([key, value]) => {
if (value) {
values[key] = value;
}
});
const user = await Users.findByIdAndUpdate(userId, {
$set: values,
$push: {
cart: {
id: args.id,
name: product.name,
type: product.type,
price: product.price,
quantity: product.quantity
}
}
}, {
new: true,
safe: true,
upsert: true
}).exec()
.catch((err) => {
console.log(err)
});
return user;