Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/node.js/37.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Node.js 猫鼬模式中的子对象不会保存递增的数字,但只保存一次_Node.js_Mongodb_Mongoose - Fatal编程技术网

Node.js 猫鼬模式中的子对象不会保存递增的数字,但只保存一次

Node.js 猫鼬模式中的子对象不会保存递增的数字,但只保存一次,node.js,mongodb,mongoose,Node.js,Mongodb,Mongoose,我有一个用户模式,我可以增加数字并保存到DB,但是对于数组中模式的子项,我也可以增加数字,但它不会保存到DB const userSchema = new Schema({ inDex: {type: Number, default: 0}, userName: { type: String, require: true, unique: true, }, e

我有一个用户模式,我可以增加数字并保存到DB,但是对于数组中模式的子项,我也可以增加数字,但它不会保存到DB

 const userSchema = new Schema({
        inDex: {type: Number, default: 0},
        userName: {
            type: String,
            require: true,
            unique: true,
        },
        email: {
            type: String,
            lowercase: true,
            unique: false,
        },
        password: { type: String, required: true },
        mnemonic: {
            type: String,
            required: true,
        },
        profiles: [address: {index: {type: Number, default: 0}]
    })

    const User = mongoose.model('user', userSchema);

    async function processUserInput(req, res) {


        User.findById({ _id: userId}).then((doc)=> {
            doc.inDex = doc.inDex+1   // Will Increment and Save
            doc.profiles[0].address.index = 
            doc.profiles[0].address.index+1   //Will increment BUT WONT SAVE

            doc.save()
        }).catch(err => console.log('err', err))

    }

    router.post('/',  async (req, res) => {
      await processUserInput(req,res)

      res.status(200).json(some json data)
    })

Document.prototype.save()
返回一个承诺,该承诺应该从承诺处理函数返回,因此
doc.save()
->
return doc.save()
函数也应该返回一个承诺,因此,在
User.findById
Document.prototype.save()之前添加一个
return
语句
返回承诺,承诺应该从承诺处理函数返回,因此
doc.save()
->
return doc.save()
函数也应该返回承诺,因此,在
User.findById