小编sac*_*hal的帖子

在 NodeJs 中向客户端发送错误时出错

我在 nodeJS 中使用异步,在最后的回调中,我正在处理错误并尝试将其发送回我的角度控制器。

 function (err, data) {
        if (err) {
            console.log(err);
            res.status(500).send({ err : err});
        }
        else {
            res.json({data: data});
        }
    });
Run Code Online (Sandbox Code Playgroud)

现在控制台中的错误是。

[Error: Username is already in use]
Run Code Online (Sandbox Code Playgroud)

我无法在我的角度控制器中收到此特定错误我尝试以所有组合(例如 .

   res.status(500).send({ err : err[0]});
      res.status(500).send({ err : err.Error});
Run Code Online (Sandbox Code Playgroud)

这就是我在前端得到的。

   Object {data: Object, status: 500, config: Object, statusText: "Internal Server Error"}
config
:
Object
data
:
Object
err
:
Object
__proto__
:
Object
__proto__
:
Object
headers
:
(d)
status
:
500
statusText
:
"Internal Server Error"
Run Code Online (Sandbox Code Playgroud)

如何将该用户名使用错误带到我的前端。

javascript node.js angularjs

5
推荐指数
1
解决办法
3705
查看次数

使用ui-router服务在离子框架中使用ui-sref和href(在哪里使用)之间的区别

我正在研究离子骨架.所以我对使用ui-sref和href很困惑.例如,对于制表符,我们使用ui-sref,因为我们可以将各种状态都链接到一些主(基本)URL.

例如

  .state('dashboard', {
    url: "/dashboard",
    templateUrl: 'templates/dashboard.html',
    controller: 'dashboardCtrl'
})

 .state('dashboard.active', {
    url: '/active',
     views: {
              'active': {
                templateUrl:  'templates/active.html',
                controller: 'ActiveCtrl'
              }
            }

})
Run Code Online (Sandbox Code Playgroud)

如果我想从这些状态或模板之一(例如,到active.html)移动到不同的模板,我的仪表板页面有标签,它现在有各种不同的状态.

//active.html

 <li class="item">
   <a class="button button-positive" ui-sref="dashboard.editClaim"> Edit Claim
   </a>
 </li>
Run Code Online (Sandbox Code Playgroud)

要么

 <li class="item">
   <a class="button button-positive" href="#/dashboard/editClaim"> Edit Claim
   </a>
 </li>
Run Code Online (Sandbox Code Playgroud)

我应该使用ui-sref或href.editclaim模板也有选项卡,如果我在那里使用ui-sref,它会正常工作,因为目前这是问题所在.所以我想知道我是否必须保持一些状态直到那里.谢谢.

javascript angularjs ionic-framework

4
推荐指数
1
解决办法
4933
查看次数

nodejs在后台发送带有图像的邮件

我想像这样发送邮件 在此处输入图片说明

现在我正在使用 nodemailer 和 email-templates 来完成这项工作,但我无法设计一个带有图像和背景徽标的模板。我也读过 sendgrid,但我不知道如何传输图像。关于我应该寻找什么的任何帮助或想法

email node.js

2
推荐指数
1
解决办法
4970
查看次数

在nodejs的router.route()中设置中间件(快速)

我想要它做什么。

 router.post('/xxxx', authorize , xxxx);

  function authorize(req, res, next)
   {
    if(xxx)
        res.send(500);
    else
     next(); 
   }
Run Code Online (Sandbox Code Playgroud)

我想检查每个路线中的会话。但是由于路由器是以这种方式编写的。

router.route('/xxx/xxxx').post(function(req, res) {
    // blah lah here...
    //
});
Run Code Online (Sandbox Code Playgroud)

因此,我该如何设置一个中间件来检查会话,我想使事情变得更通用,并希望有一个单一的授权功能来完成一件事情而不是检查每个请求。

node.js express

1
推荐指数
2
解决办法
6173
查看次数

mongodb schema.createIndex 不是函数

所以我试图在我的 messageSchema 中创建一个索引

    var messageSchema = new Schema({
    senderName : String,
    content : String,
    reply : String,
    date: { type: Date, default: Date.now() },
    room : { type: Schema.Types.ObjectId }
});
Run Code Online (Sandbox Code Playgroud)

// 这是尝试创建索引的方法

 messageSchema.createIndex({content : "text"}, function(err, data){
     console.log(err);
     console.log(data);
    });
Run Code Online (Sandbox Code Playgroud)

//也试过

  messageSchema.createIndex({content : "text"}); 
Run Code Online (Sandbox Code Playgroud)

//也试过这个

 messageSchema.createIndex({"content" : "text"});
Run Code Online (Sandbox Code Playgroud)

我不断收到的错误是

TypeError: messageSchema.createIndex is not a function

谁能帮我这个。

mongoose mongodb node.js

1
推荐指数
1
解决办法
7083
查看次数

Mongodb在模式级别生成默认随机数生成相同的数字

我使用mongodb作为数据库,使用mongoose作为ORM.我的架构中有一个字段booking_id,这是唯一的,所以我不能将它取消.因此我设计了类似这样的代码.

  var bookingSchema = new Schema({
    booking_id_customer: {
        type: Number,
        default : Math.floor(Math.random()*900000000300000000000) + 1000000000000000,
        index: { unique: true }
    },
Run Code Online (Sandbox Code Playgroud)

它第一次完美运行,但从第2次开始,我得到了这个重复错误.

    { [MongoError: E11000 duplicate key error index: xx.bookings.$booking_id_customer_1 dup key: { : 4.439605615108491e+20 }]
  name: 'MongoError',
  message: 'E11000 duplicate key error index:
Run Code Online (Sandbox Code Playgroud)

我希望它能产生随机数,但我不知道第二次出现什么问题.

javascript mongoose mongodb node.js

1
推荐指数
1
解决办法
2300
查看次数