te')); return $arr; } /* 遍历用户所有主题 * @param $uid 用户ID * @param int $page 页数 * @param int $pagesize 每页记录条数 * @param bool $desc 排序方式 TRUE降序 FALSE升序 * @param string $key 返回的数组用那一列的值作为 key * @param array $col 查询哪些列 */ function thread_tid_find_by_uid($uid, $page = 1, $pagesize = 1000, $desc = TRUE, $key = 'tid', $col = array()) { if (empty($uid)) return array(); $orderby = TRUE == $desc ? -1 : 1; $arr = thread_tid__find($cond = array('uid' => $uid), array('tid' => $orderby), $page, $pagesize, $key, $col); return $arr; } // 遍历栏目下tid 支持数组 $fid = array(1,2,3) function thread_tid_find_by_fid($fid, $page = 1, $pagesize = 1000, $desc = TRUE) { if (empty($fid)) return array(); $orderby = TRUE == $desc ? -1 : 1; $arr = thread_tid__find($cond = array('fid' => $fid), array('tid' => $orderby), $page, $pagesize, 'tid', array('tid', 'verify_date')); return $arr; } function thread_tid_delete($tid) { if (empty($tid)) return FALSE; $r = thread_tid__delete(array('tid' => $tid)); return $r; } function thread_tid_count() { $n = thread_tid__count(); return $n; } // 统计用户主题数 大数量下严谨使用非主键统计 function thread_uid_count($uid) { $n = thread_tid__count(array('uid' => $uid)); return $n; } // 统计栏目主题数 大数量下严谨使用非主键统计 function thread_fid_count($fid) { $n = thread_tid__count(array('fid' => $fid)); return $n; } ?>javascript - TypeError: callback.apply is not a function (Node.js & Mongodb) - Stack Overflow
最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - TypeError: callback.apply is not a function (Node.js & Mongodb) - Stack Overflow

programmeradmin3浏览0评论

When I add the line "{ upsert: true }", I got this error:

TypeError: callback.apply is not a function

// on routes that end in /users/petitorAnalysisTextData
// ----------------------------------------------------

router
  .route('/users/petitorAnalysisTextData/:userName')

  // update the user info (accessed at PUT http://localhost:8080/api/users/petitorAnalysisTextData)
  .post(function (req, res) {
    // use our user model to find the user we want
    User.findOne({userName: req.params.userName}, function (err, user) {
      if (err) res.send(err);

      console.log(
        'userpetitorAnalysis.firstObservation: %@',
        userpetitorAnalysis.firstObservation,
      );
      // Got the user name
      var userName = user.userName;
      // update the text data
      console.log('Baobao is here!');
      user.update(
        {
          userName: userName,
        },
        {
          $set: {
            'petitorAnalysis.firstObservation': req.body.firstObservation,
            'petitorAnalysis.secondObservation': req.body.secondObservation,
            'petitorAnalysis.thirdObservation': req.body.thirdObservation,
            'petitorAnalysis.brandName': req.body.brandName,
            'petitorAnalysis.productCategory': req.body.productCategory,
          },
        },
        {upsert: true},
      );

      // save the user
      user.save(function (err) {
        if (err) return res.send(err);

        return res.json({message: 'User updated!'});
      });
    });
  });

Without this line, there is no error. I'm new to nodejs, not very sure where the problem is.

Update

No error message now, but this part of the database is not updated with new data. The embedded document is still empty.

// on routes that end in /users/petitorAnalysisTextData
// ----------------------------------------------------
router
  .route('/users/petitorAnalysisTextData/:userName')

  // update the user info (accessed at PUT http://localhost:8080/api/users/petitorAnalysisTextData)
  .post(function (req, res) {
    console.log('1');

    // Just give instruction to mongodb to find document, change it;
    // then finally after mongodb is done, return the result/error as callback.
    User.findOneAndUpdate(
      {userName: req.params.userName},
      {
        $set: {
          'petitorAnalysis.firstObservation': req.body.firstObservation,
          'petitorAnalysis.secondObservation': req.body.secondObservation,
          'petitorAnalysis.thirdObservation': req.body.thirdObservation,
          'petitorAnalysis.brandName': req.body.brandName,
          'petitorAnalysis.productCategory': req.body.productCategory,
        },
      },
      {upsert: true},
      function (err, user) {
        // after mongodb is done updating, you are receiving the updated file as callback
        console.log('2');
        // now you can send the error or updated file to client
        if (err) return res.send(err);

        return res.json({message: 'User updated!'});
      },
    );
  });

When I add the line "{ upsert: true }", I got this error:

TypeError: callback.apply is not a function

// on routes that end in /users/petitorAnalysisTextData
// ----------------------------------------------------

router
  .route('/users/petitorAnalysisTextData/:userName')

  // update the user info (accessed at PUT http://localhost:8080/api/users/petitorAnalysisTextData)
  .post(function (req, res) {
    // use our user model to find the user we want
    User.findOne({userName: req.params.userName}, function (err, user) {
      if (err) res.send(err);

      console.log(
        'user.petitorAnalysis.firstObservation: %@',
        user.petitorAnalysis.firstObservation,
      );
      // Got the user name
      var userName = user.userName;
      // update the text data
      console.log('Baobao is here!');
      user.update(
        {
          userName: userName,
        },
        {
          $set: {
            'petitorAnalysis.firstObservation': req.body.firstObservation,
            'petitorAnalysis.secondObservation': req.body.secondObservation,
            'petitorAnalysis.thirdObservation': req.body.thirdObservation,
            'petitorAnalysis.brandName': req.body.brandName,
            'petitorAnalysis.productCategory': req.body.productCategory,
          },
        },
        {upsert: true},
      );

      // save the user
      user.save(function (err) {
        if (err) return res.send(err);

        return res.json({message: 'User updated!'});
      });
    });
  });

Without this line, there is no error. I'm new to nodejs, not very sure where the problem is.

Update

No error message now, but this part of the database is not updated with new data. The embedded document is still empty.

// on routes that end in /users/petitorAnalysisTextData
// ----------------------------------------------------
router
  .route('/users/petitorAnalysisTextData/:userName')

  // update the user info (accessed at PUT http://localhost:8080/api/users/petitorAnalysisTextData)
  .post(function (req, res) {
    console.log('1');

    // Just give instruction to mongodb to find document, change it;
    // then finally after mongodb is done, return the result/error as callback.
    User.findOneAndUpdate(
      {userName: req.params.userName},
      {
        $set: {
          'petitorAnalysis.firstObservation': req.body.firstObservation,
          'petitorAnalysis.secondObservation': req.body.secondObservation,
          'petitorAnalysis.thirdObservation': req.body.thirdObservation,
          'petitorAnalysis.brandName': req.body.brandName,
          'petitorAnalysis.productCategory': req.body.productCategory,
        },
      },
      {upsert: true},
      function (err, user) {
        // after mongodb is done updating, you are receiving the updated file as callback
        console.log('2');
        // now you can send the error or updated file to client
        if (err) return res.send(err);

        return res.json({message: 'User updated!'});
      },
    );
  });

Share Improve this question edited Oct 7, 2020 at 9:48 Anhdevit 2,1043 gold badges18 silver badges35 bronze badges asked Sep 2, 2016 at 19:21 Chenya ZhangChenya Zhang 4633 gold badges11 silver badges22 bronze badges 0
Add a ment  | 

2 Answers 2

Reset to default 11

There are 2 ways to update documents in mongodb:

  1. find the document, bring it to server, change it, then save it back to mongodb.

  2. just give instruction to mongodb to find document, change it; then finally after mongodb is done, return the result/error as callback.

In your code, you are mixing both methods.


  1. with user.save(), first you search the database with user.findOne, and pull it to server(nodejs), now it lives in your puter memory. then you can manually change the data and finally save it to mongodb with user.save()

    User.findOne({ userName: req.params.userName}, function(err, user) {
    
        if (err)
            res.send(err);
    
        //this user now lives in your memory, you can manually edit it
        user.username = "somename";
        user.petitorAnalysis.firstObservation = "somethingelse";
    
        // after you finish editing, you can save it to database or send it to client
         user.save(function(err) {
            if (err)
                return res.send(err);
    
            return res.json({ message: 'User updated!' });
        });
    
  2. the second one is to use User.findOneAndUpdate().. This is preferred, instead of user.findOne() then user.update(); because those basically searching the database twice. first to findOne(), and search again to update()

Anyway,the second method is telling mongodb to update the data without first bringing to server, Next, only after mongodb finish with its action, you will receive the updated-file (or error) as callback

User.findOneAndUpdate({ userName: req.params.userName}, 
            {
             $set: { "petitorAnalysis.firstObservation" : req.body.firstObservation,
                      "petitorAnalysis.secondObservation" : req.body.secondObservation,
                      "petitorAnalysis.thirdObservation" : req.body.thirdObservation,
                      "petitorAnalysis.brandName" : req.body.brandName,
                      "petitorAnalysis.productCategory" : req.body.productCategory
            } },
            { upsert: true },
        function(err, user) {
        //after mongodb is done updating, you are receiving the updated file as callback    

        // now you can send the error or updated file to client
        if (err)
            res.send(err);

        return res.json({ message: 'User updated!' });
        });

You forgot to pass a callback to the update method

user.update(
  {
    $set: {
      'petitorAnalysis.firstObservation': req.body.firstObservation,
      'petitorAnalysis.secondObservation': req.body.secondObservation,
      'petitorAnalysis.thirdObservation': req.body.thirdObservation,
      'petitorAnalysis.brandName': req.body.brandName,
      'petitorAnalysis.productCategory': req.body.productCategory,
    },
  },
  {upsert: true},
  function (err, result) {},
);

update method expects 3 arguments.

  • document update
  • options
  • callback
发布评论

评论列表(0)

  1. 暂无评论